mirror of
https://github.com/prdlk/leetcode.git
synced 2026-11-02 21:22:43 +00:00
46 lines
2.4 KiB
Plaintext
46 lines
2.4 KiB
Plaintext
---
|
|
title: Prefix Sum
|
|
description: 'A running total is stored at each index, so the sum of any range is one subtraction.'
|
|
hidden: true
|
|
---
|
|
|
|
Suppose you are handed an array and asked for the sum of the elements between index `i` and index `j`. One query is easy: loop from `i` to `j` and add. But now imagine thousands of such queries over the same array, or a search that inspects the sum of every possible subarray. Recomputing each range from scratch costs `O(n)` per query, and the work piles up fast.
|
|
|
|
Prefix sum removes that repetition. You spend `O(n)` once to precompute running totals, and afterward every range sum collapses into a single subtraction that runs in `O(1)`, no matter how wide the range.
|
|
|
|
<YouTube id="CRVxUgmIO8M" title="Prefix Sum Explained" />
|
|
|
|
## Transformation
|
|
|
|
A prefix sum turns an array into a new array of running totals. For an input array `arr` of length `n`, build a `prefix` array of length `n + 1`:
|
|
|
|
`prefix[0] = 0`
|
|
|
|
`prefix[k] = arr[0] + arr[1] + ... + arr[k-1]`
|
|
|
|
Each entry is the previous total plus one more element, so a single pass builds the whole thing:
|
|
|
|
`prefix[k] = prefix[k-1] + arr[k-1]`
|
|
|
|
The leading `prefix[0] = 0` is the sum of zero elements. That extra slot is what makes the range math below land exactly on the boundaries, which is why `prefix` has one more cell than `arr`.
|
|
|
|
|
|
<Frame caption="Each bar is the running total up to that index. Every step keeps the previous total (blue) and adds one new element (amber)" hint="">
|
|
|
|

|
|
|
|
|
|
</Frame>
|
|
|
|
## Any range sum is one subtraction
|
|
|
|
The sum of the subarray from index `i` up to (but not including) `j` is `prefix[j] - prefix[i]`. `prefix[j]` is the total of everything before `j`, and `prefix[i]` is the total of everything before `i`, so subtracting throws away the front you did not ask for and leaves the middle.
|
|
|
|
## Watch the boundaries
|
|
|
|
The one place prefix sums trip people up is the off-by-one at the edges. Keep three facts straight: `prefix` has length `n + 1`, `prefix[0]` is `0`, and `sum(arr[i..j-1]) = prefix[j] - prefix[i]` uses an exclusive right index `j`. If a problem hands you an inclusive right index `r`, translate it to `prefix[r + 1] - prefix[i]`.
|
|
|
|
## When to reach for it
|
|
|
|
Prefix sums fit whenever you need repeated range sums over a fixed array, or when an algorithm searches across subarray boundaries. For a single one-off sum, a plain loop is simpler; the prefix array earns its `O(n)` setup only once the same array is queried many times.
|