Loading…
Loading…
Charting your lesson…
Opening your lesson…
Prefix Sum and Difference Arrays
Lesson 4 of 8
6 steps•about 11 min•11 flashcards
Step 1/ 61 min
Prefix sums answer "sum of a range" in O(1) after O(n) setup, and their mirror image — the difference array — applies many range updates in O(1) each. Together they turn repeated range work from O(n) per query into constant time.
Core idea: precompute cumulative totals once so any range aggregate becomes a single subtraction. The difference array does the reverse: record only changes, then integrate at the end.
Step 2/ 61 min
Define P[0] = 0 and P[i] = a[0] + … + a[i-1]. Then the sum of a[l..r] (inclusive) is:
rangeSum(l, r) = P[r+1] - P[l]
Building P is O(n); every range-sum query is then O(1). With Q queries, brute force is O(n·Q); prefix sums make it O(n + Q).
Combine prefix sums with a hash map to count subarrays summing to K in one pass. A subarray a[l..r] sums to K exactly when P[r+1] − P[l] = K, i.e. P[l] = P[r+1] − K. Store how many times each prefix value has occurred:
count = 0; seen = {0: 1}; run = 0
for x in a:
run += x
count += seen.get(run - K, 0)
seen[run] = seen.get(run, 0) + 1
O(n) time, O(n) space — and it works with negative numbers, where sliding windows fail.
To add v to every element in [l, r] many times, don't touch the range each time. Keep diff, do diff[l] += v; diff[r+1] -= v (O(1) per update). After all updates, the prefix sum of diff reconstructs the final array in O(n). So m range updates cost O(m + n) instead of O(m·n). This is the 1-D version of what 2-D summed-area tables do for grids.
Step 3/ 61 min
Array [2, 4, 1, 3], find sum of indices 1..3. P = [0, 2, 6, 7, 10]. rangeSum(1,3) = P[4] − P[1] = 10 − 2 = 8 (= 4+1+3). One subtraction, no loop.
Step 4/ 61 min
Step 5/ 61 min
n+1 and index carefully — the P[l] vs P[l−1] off-by-one is the classic bug.{0: 1} so subarrays starting at index 0 are counted.sum = P[r2][c2] − P[r1-1][c2] − P[r2][c1-1] + P[r1-1][c1-1] (inclusion–exclusion).Forgetting the seen = {0: 1} seed in the subarray-sum-equals-K counter. Without it, any subarray that starts at index 0 and hits the target is missed, because no earlier prefix of 0 was recorded.
Step 6/ 6· Worked examples1 min
Problem
Using a prefix-sum array, find the sum of elements from index 1 to 3 (inclusive) in arr = [2, 4, 1, 3, 5].
Try it on paper first.
Recall
Keep this
Usage: precompute prefix sums when many range-sum queries hit a static array.
Prefix sums and difference arrays are the preprocessing tricks that make range queries and range updates cheap. They trade a one-time O(n) build for O(1) per query or update — a classic space-for-time and preprocess-for-query bargain that appears constantly.
| Technique | Build | Query/Update | Space |
|---|---|---|---|
| 1D prefix sum | O(n) | O(1) range sum | O(n) |
| Difference array | O(n) at end | O(1) range add | O(n) |
| 2D prefix sum | O(mn) | O(1) submatrix | O(mn) |
| Prefix + hashmap | O(n) | O(n) subarray-sum-k | O(n) |
Repeated range-sum queries, "count subarrays with sum k", equilibrium/pivot index, or many interval increments followed by a final snapshot. The interviewer checks that you preprocess instead of recomputing per query.
Prefix sum: O(n) build → O(1) range sums; pair with a hash map for subarray-sum-k in O(n). Difference array: O(1) range adds, reconstruct with one pass. Extend both to 2D via inclusion-exclusion.
You've read 0 of 6 steps
Now lock it in — questions straight after reading are what make it stick.