Range Addition - Problem

Imagine you're a game developer working on a score multiplier system. You start with an array of zeros representing base scores, and you need to apply multiple range updates to boost scores in specific segments.

Given an integer length and an array updates where updates[i] = [startIdx, endIdx, inc], you have an array arr of length length with all zeros initially.

For each update operation, you need to increment all elements from arr[startIdx] to arr[endIdx] (inclusive) by inc.

Goal: Return the final array after applying all updates efficiently.

Example: If length = 5 and updates = [[1,3,2],[2,4,3],[0,2,-2]], the final array should be [-2, 0, 3, 5, 3].

Input & Output

example_1.py โ€” Basic Range Updates
$ Input: length = 5, updates = [[1,3,2],[2,4,3],[0,2,-2]]
โ€บ Output: [-2, 0, 3, 5, 3]
๐Ÿ’ก Note: Start with [0,0,0,0,0]. After [1,3,2]: [0,2,2,2,0]. After [2,4,3]: [0,2,5,5,3]. After [0,2,-2]: [-2,0,3,5,3].
example_2.py โ€” Single Update
$ Input: length = 10, updates = [[2,4,6]]
โ€บ Output: [0,0,6,6,6,0,0,0,0,0]
๐Ÿ’ก Note: Only one update operation adds 6 to positions 2, 3, and 4. All other positions remain 0.
example_3.py โ€” Empty Updates
$ Input: length = 3, updates = []
โ€บ Output: [0,0,0]
๐Ÿ’ก Note: No updates applied, so the array remains all zeros as initialized.

Visualization

Tap to expand
๐Ÿจ Hotel Elevator Weight TrackingBrute Force Approach:For each group, update weight at EVERY floor they travel throughTime: O(groups ร— floors) - Very slow for many groups!Smart Approach (Difference Array):Mark only when groups get ON (+weight) and get OFF (-weight)Time: O(groups + floors) - Much faster!Example: Groups moving in elevator+50kgFloor 1-30kgFloor 3+20kgFloor 0Group travelsResult after one sweep:2070704040F0F1F2F3F4
Understanding the Visualization
1
Mark Passenger Movements
Instead of tracking weight at every floor, just mark when passengers get ON (+weight) and get OFF (-weight)
2
One Sweep Calculation
Make one pass through all floors, keeping a running total of weight changes
3
Efficient Result
This gives us the weight at each floor without processing every floor for every group
Key Takeaway
๐ŸŽฏ Key Insight: Transform expensive range operations into cheap boundary markers + one sweep. This is the essence of difference arrays!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(k + n)

Where k is the number of updates and n is the array length. We process each update in O(1) and then do one O(n) prefix sum pass

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

We need the difference array of size n (or n+1 to handle end+1 safely)

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค length โ‰ค 105
  • 0 โ‰ค updates.length โ‰ค 104
  • 0 โ‰ค startIdx โ‰ค endIdx < length
  • -1000 โ‰ค inc โ‰ค 1000
Asked in
Google 45 Facebook 38 Amazon 32 Microsoft 28
87.3K Views
Medium Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen