How to recognise
the sum of a contiguous range: how many subarrays sum to k, many questions about the sum from i to j, or negative numbers that break a sliding window.
How to solve problems like this
- Rewrite the sum of a range as the difference of two running sums.
- Decide what to remember about earlier prefixes: how often each appeared, its first index, or only that it appeared.
- Put the empty prefix, 0, in the map first, so ranges that start at index 0 count too.
- For each number: update the running sum, look up the prefix you need, then store the current one.
Variant: From both sides
The same, but build up from both sides: one pass from the left, one from the right. Use it when the answer at a position depends on everything to its left and everything to its right.
- Reach for it when the answer at i depends on everything before i and everything after i.
- Pass from the left and store, for each index, what lies before it: a product, a sum, a maximum.
- Pass from the right with one running value for what lies after it, and combine the two.
- The same two passes give the highest wall to the left and to the right of each spot when you count trapped rain water, and they settle a two-sided rule: hand out candies so that a higher rating beats both neighbours.
Where it's used
- Face detection. The Viola–Jones detector, which ships with OpenCV, needs the brightness sum of many rectangles. It first builds a table of running sums, and then the sum of any rectangle takes four numbers from it.
- Sorting on a GPU. Radix sort counts how many numbers fall into each bucket, and the running sums of those counts say where each bucket starts in the output. NVIDIA's CUB library sorts this way.
- SQL. The window function SUM(...) OVER (ORDER BY ...) gives a running total: for each row, the sum of all rows up to it. Those are exactly the running sums.