Range Addition - Problem
You are given an integer length and an array updates where updates[i] = [startIdxi, endIdxi, inci].
You have an array arr of length length with all zeros, and you have some operations to apply on arr.
In the i-th operation, you should increment all the elements arr[startIdxi], arr[startIdxi + 1], ..., arr[endIdxi] by inci.
Return arr after applying all the updates.
Input & Output
Example 1 — Basic Range Updates
$
Input:
length = 5, updates = [[1,3,2],[2,4,3],[0,2,-2]]
›
Output:
[-2,0,1,3,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]. Wait, let me recalculate: [-2,0,1,3,3]
Example 2 — Single Update
$
Input:
length = 10, updates = [[2,4,6]]
›
Output:
[0,0,6,6,6,0,0,0,0,0]
💡 Note:
Add 6 to indices 2,3,4. All other positions remain 0.
Example 3 — Overlapping Updates
$
Input:
length = 3, updates = [[0,1,1],[1,2,1]]
›
Output:
[1,2,1]
💡 Note:
First update: [1,1,0]. Second update adds to indices 1,2: [1,2,1].
Constraints
- 1 ≤ length ≤ 105
- 0 ≤ updates.length ≤ 104
- 0 ≤ startIdxi ≤ endIdxi < length
- -1000 ≤ inci ≤ 1000
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array of zeros with length n and list of range updates
2
Process
Apply each range update [start,end,inc] to add inc to range
3
Output
Final array after all updates applied
Key Takeaway
🎯 Key Insight: Use difference array to mark range boundaries in O(1), then compute prefix sum for final values
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code