Prefix Sums
A prefix-sum array stores the running total up to each index so any range sum arr[i] + ... + arr[j] becomes two lookups and a subtraction, O(1) after O(n) setup.
Detailed Explanation
Summing arr[i]..=arr[j] in a loop is O(j - i). Doing that for q queries is O(q n). If the array does not change, precompute prefixes.
Let pref[0] = 0 and pref[k] = arr[0] + ... + arr[k - 1]. Then the sum of arr[i]..arr[j] (inclusive) is pref[j + 1] - pref[i]. The extra zero at the front means you do not special-case i === 0. An off-by-one here is the usual bug.
You can store prefixes in the same array if you are allowed to destroy it: arr[k] += arr[k - 1]. Then the formula changes and you have to remember you overwrote the data.
Prefix sums extend to xor, counts, and 2D grids (summed-area tables). The idea is the same: a linear pass, then O(1) or O(1) per rectangle with four corners. Difference arrays are the inverse trick: you add a delta at i and subtract it at j + 1, then one prefix pass materialises the array. That is how you apply many range updates in O(n + q).
Prefix sums need an associative, invertible operation. Sum and xor work. Min and gcd do not (you cannot subtract a min). For those, use a segment tree or a sparse table.
JavaScript numbers are IEEE doubles. Long prefix sums of integers can leave the safe integer range. If you sum money or large ids, consider BigInt prefixes.
What it costs you: O(n) extra memory and a stale prefix if the array later mutates. If updates and queries mix, you need a Fenwick tree or segment tree, not a one-shot prefix array.
Reach for prefix sums when you answer many range-sum queries on a frozen array, or when a sliding-window sum is easier as pref[right+1] - pref[left]. Reach for a running variable when you have a single left-to-right pass and never need an old range.
Code Examples
function prefixSums(arr) {
const pref = new Array(arr.length + 1);
pref[0] = 0;
for (let i = 0; i < arr.length; i += 1) {
pref[i + 1] = pref[i] + arr[i];
}
return pref;
}
function rangeSum(pref, i, j) {
return pref[j + 1] - pref[i];
}
const pref = prefixSums([2, 3, 7, 1]);
console.log(rangeSum(pref, 1, 3)); // => 11 (3+7+1)
console.log(rangeSum(pref, 0, 0)); // => 2function applyRangeAdds(n, updates) {
const diff = new Array(n + 1).fill(0);
for (const [i, j, delta] of updates) {
diff[i] += delta;
diff[j + 1] -= delta;
}
const out = new Array(n);
let running = 0;
for (let k = 0; k < n; k += 1) {
running += diff[k];
out[k] = running;
}
return out;
}
console.log(applyRangeAdds(5, [[1, 3, 2]])); // => [ 0, 2, 2, 2, 0 ]Common Gotchas
Inclusive/exclusive ends
decide whether `pref[k]` includes `arr[k]` and stick to it. The `pref[0] = 0` convention makes `sum(i, j) = pref[j+1] - pref[i]`.
Updates invalidate prefixes
recompute or use a tree. Do not query after a silent `arr[i] = x`.
Doubles lose integer precision
long sums of large integers need `BigInt` or a scaled integer strategy.
Min/gcd prefixes are not invertible
you cannot recover a range min with two prefix mins.