Range Sum Query
The sum of a range is the total up to its end minus the total before its start.
Result · step 9 of 11Prefix Sum and Kadane: Range sum from prefix totals
prefix[7] = prefix[6] + a[7] = 25 + 6 = 31.
What you will see
Two prefix cells light up; the answer is their difference.
Cost
| Best | O(1) |
|---|---|
| Average | O(1) |
| Worst | O(1) |
| Space | O(n) |
After O(n) preprocessing.
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
Prefix Sum and Kadane covers these too, in the same run.