Back to ArraysRange Addition
Medium

Range Addition

MediumRecommendedDifference Array

Given an array length and a list of range updates (each adding a delta to every element in an inclusive index range), return the final array after applying every update.

Examples

Example 1

Input:
length = 5, updates = [[1,3,2],[2,4,3],[0,2,-2]]
Output:
[-2,0,3,5,3]

Explanation: The three updates overlap across several indices; the difference array accumulates all of their boundary effects before one prefix-sum pass reconstructs the final values.

Example 2

Input:
length = 4, updates = [[0,3,5]]
Output:
[5,5,5,5]

Explanation: The range ends exactly at the array's last index, so there is no end + 1 position to subtract at — every element receives the delta.

Example 3

Input:
length = 3, updates = [[0,0,1],[2,2,2]]
Output:
[1,0,2]

Explanation: Two non-overlapping single-index ranges each affect only their own position, with 0 left untouched in between.

Constraints

  • 1 <= length <= 10^5
  • 0 <= updates.length <= 10^4
  • 0 <= start <= end <= length - 1
  • -1000 <= delta <= 1000

Loading editor…

Code execution is coming soon.