Prefix sums
A prefix sum array stores the sum of every prefix of the input. The sum of a range [left, right] then becomes one subtraction: prefix[right + 1] - prefix[left].
Detailed Explanation
Asking for the sum of many subarrays with a loop each time is O(n) per query. If you first build prefix[0] = 0 and prefix[i + 1] = prefix[i] + xs[i], every range sum is O(1). Building the prefix is O(n). That trade pays off after a handful of queries, and it is the backbone of many interview problems (subarray sum equals k, range sums in a matrix).
Inclusive-exclusive indexing avoids off-by-one. prefix[i] is the sum of the first i elements, so xs[0..i) is prefix[i]. The closed range xs[L..R] is prefix[R + 1] - prefix[L]. Put the extra 0 at the front so L = 0 needs no branch.
The same idea works for XOR (prefix XOR), counts of a predicate (prefix of 0/1), and two-dimensional grids (inclusion of rectangles). Difference arrays are the inverse: you store increments at the start and end+1 of updates, then prefix them to recover the array. That turns Q range updates into O(Q + n).
You reach for prefix sums when you need many range aggregates and the values do not change, or when you convert range updates into point updates. You keep a segment tree or a Fenwick tree when values change and you still need range queries. You keep a sliding window when there is one moving range and a condition, not arbitrary ranges.
The cost is O(n) extra memory and overflow. Sums of ints should live in long[]. The cost of a wrong index convention is a silent off-by-one that passes the sample and fails the hidden tests. Write the empty prefix as 0 and stick to it. Negative numbers are fine; "subarray sum equals k" uses a map of prefix frequencies, not a window (the sum is not monotonic).
Do not mutate xs in place as the prefix unless you no longer need the original. Interview code is clearer with a separate array.
Code Examples
public class RangeSum {
static long[] prefix(int[] xs) {
long[] p = new long[xs.length + 1];
for (int i = 0; i < xs.length; i++) {
p[i + 1] = p[i] + xs[i];
}
return p;
}
static long sum(long[] p, int left, int right) { // inclusive
return p[right + 1] - p[left];
}
public static void main(String[] args) {
long[] p = prefix(new int[] {2, 3, 7, 1});
System.out.println(sum(p, 1, 2)); // prints: 10 (3+7)
System.out.println(sum(p, 0, 3)); // prints: 13
}
}import java.util.HashMap;
import java.util.Map;
public class SubarrayK {
static int count(int[] xs, int k) {
Map<Long, Integer> seen = new HashMap<>();
seen.put(0L, 1);
long prefix = 0;
int answer = 0;
for (int x : xs) {
prefix += x;
answer += seen.getOrDefault(prefix - k, 0);
seen.merge(prefix, 1, Integer::sum);
}
return answer;
}
public static void main(String[] args) {
System.out.println(count(new int[] {1, 1, 1}, 2)); // prints: 2
}
}Common Gotchas
`int` prefixes overflow
use `long[]` for the running sum.
Pick one convention and keep it
`prefix[i] = sum of first i` makes range `[L, R]` equal `prefix[R+1] - prefix[L]`.
Negative values break a sliding-window approach to "sum equals k"
use prefix frequencies.
In-place prefixes destroy the original array
copy if you still need `xs`.