Data Structures19 sections · 729 units
Open in Course

Range Addition - Algorithm

Two-phase approach

The algorithm has two phases:

1.1. Process all updates on the difference array. Each update modifies exactly two positions.

2.2. Compute the prefix sum of the difference array to get the final result. This separates the O(k)O(k) update phase from the O(n)O(n) reconstruction phase. Total: O(k+n)O(k + n). Time: O(k+n)O(k + n). Space: O(n)O(n).