AlgoScope

Difference Array

AlgorithmintermediateArray Techniques

Record range updates as two endpoint deltas; a final prefix sum applies them all.

Decision · step 2 of 14Difference Array: Three overlapping ranges
active 1-300510203-5405060708

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.

Open in the player →or start at step 2

What you will see

Each range update adds at l and subtracts after r; the prefix pass reveals the final array.

How difference array works →

Cost

BestO(n + q)
AverageO(n + q)
WorstO(n + q)
SpaceO(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

Leads to

Topics that need this one first.

Taught by the same lesson

Difference Array covers these too, in the same run.