Difference Array
Record range updates as two endpoint deltas; a final prefix sum applies them all.
Update 1: add 5 to indices 1 to 3. Two writes: +5 at 1, where it starts, and -5 at 4, where it stops. The 2 cells between them are never touched.
What you will see
Each range update adds at l and subtracts after r; the prefix pass reveals the final array.
Cost
| Best | O(n + q) |
|---|---|
| Average | O(n + q) |
| Worst | O(n + q) |
| Space | O(n) |
O(1) per range updateone O(n) finalization.
How you work with it here
play it through, step one change at a time, scrub to any step, run it on your own input, predict what happens next.
Screen readers: Each cell is announced as index, value and state; each step's accessibility text states the decision and its effect.
Reduced motion: Swaps and shifts become value crossfades with an outline flash; pointers appear at their destination.
Before this
Related
Taught by the same lesson
Difference Array covers these too, in the same run.