Prefix Sums
Range Queries in Java
Learn how to precompute cumulative sums so you can answer any range-sum query in constant time: the foundation of efficient array algorithms.
What Is a Prefix Sum?
A prefix sum array stores the running total of elements from index \(0\) up to each position. Given an array \(\texttt{a}\) of length \(n\), the prefix sum array \(\texttt{p}\) is defined so that:
The prefix sum array has length \( n + 1 \). The extra leading zero makes the range-query formula clean and avoids special-casing index \(0\).
Building the Prefix Array
Why Prefix Sums Matter
The Core Formula
Build:
Query: sum of \( a[L \ldots R] \) (inclusive):
Both steps use 0-indexed arrays, matching Java conventions.
Why Does the Subtraction Work?
0-Indexed vs 1-Indexed
| 0-Indexed (Java) | 1-Indexed | |
|---|---|---|
| Array range | \( a[0 \ldots n-1] \) | \( a[1 \ldots n] \) |
| Prefix length | \( n + 1 \) | \( n + 1 \) |
| Build rule | \( p[i] = p[i-1] + a[i-1] \) | \( p[i] = p[i-1] + a[i] \) |
| Query \( [L, R] \) | \( p[R+1] - p[L] \) | \( p[R] - p[L-1] \) |
How to Apply Prefix Sums
p[i] = p[i-1] + a[i-1].p[R+1] - p[L].long if sums can exceed \( 2^{31} - 1 \)).Java: Building the Prefix Array
Java: Querying a Range Sum
Example 1: Simple Range Query
Problem: Given \( a = [4, 1, 6, 2, 5] \), find the sum of \( a[0 \ldots 2] \).
Example 2: Multiple Queries
Problem: \( a = [3, -2, 7, 1, -4, 5] \). Answer queries: \([1,3]\), \([0,5]\), \([4,4]\).
| Query | Formula | Answer |
|---|---|---|
| \([1, 3]\) | \( p[4] - p[1] = 9 - 3 \) | 6 |
| \([0, 5]\) | \( p[6] - p[0] = 10 - 0 \) | 10 |
| \([4, 4]\) | \( p[5] - p[4] = 5 - 9 \) | \(-4\) |
Prefix sums handle negative values and single-element queries without any special logic.
Example 3: Full Java Solution
Common Mistakes
p[R] - p[L] instead of p[R+1] - p[L]. This misses \( a[R] \).new long[n] instead of new long[n+1]. You need that extra slot for \( p[n] \).int[] for the prefix array when the sum exceeds \( 2^{31} - 1 \). Always use long[].Summary at a Glance
| Aspect | Details |
|---|---|
| Build time | \( O(n) \) |
| Query time | \( O(1) \) per query |
| Extra space | \( O(n) \) for the prefix array |
| Build formula | \( p[i] = p[i-1] + a[i-1] \) |
| Query formula | \( p[R+1] - p[L] \) |
| Works with negatives? | Yes |
| Java type | long[] recommended |
Question 1 of 5
Given \( a = [1, 3, 5, 7] \), what is \( p[3] \) in the prefix sum array?
Question 2 of 5
Using prefix sums on a 0-indexed array, the sum of \( a[2 \ldots 5] \) is:
Question 3 of 5
What is the time complexity of answering \( q \) range-sum queries after building a prefix sum array on an array of length \( n \)?
Question 4 of 5
In Java, why should the prefix sum array typically be long[] instead of int[]?
int, adding many of them together can exceed \( 2^{31} - 1 \approx 2.1 \times 10^9 \). Using long supports sums up to about \( 9.2 \times 10^{18} \).Question 5 of 5
Code snippet:
int[] a = {10, 20, 30, 40, 50};
long[] p = new long[a.length + 1];
for (int i = 1; i <= a.length; i++)
p[i] = p[i-1] + a[i-1];
System.out.println(p[4] - p[1]);
What does the code print?