01 / 20
\(\Sigma\)

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.

Java 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

a[] 2 5 1 3 7 0 1 2 3 4 cumulate p[] 0 2 7 8 11 18 0 1 2 3 4 5
\(\text{sum}(2..4) = p[5] - p[2] = 18 - 7 = 11\)
\(a[2] + a[3] + a[4] = 1 + 3 + 7 = 11 \ \checkmark\)

Why Prefix Sums Matter

Brute Force
Each range query loops from \(L\) to \(R\): \(O(n)\) per query. With \(q\) queries: \(O(n \cdot q)\).
Prefix Sums
Build once in \(O(n)\), then answer each query in \(O(1)\). Total: \(O(n + q)\).

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?

\(p[R+1] = a[0] + \dots + a[L-1] + a[L] + \dots + a[R]\)
\(p[L] = a[0] + \dots + a[L-1]\)
\(\text{Subtract: } p[R+1] - p[L] = a[L] + \dots + a[R]\)
The leading terms cancel: telescoping!

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

1
Allocate a prefix array of size \( n + 1 \) and set \( p[0] = 0 \).
2
Fill it in one pass: p[i] = p[i-1] + a[i-1].
3
Answer each query \( [L, R] \) with p[R+1] - p[L].
4
Watch out for edge cases: empty ranges, full array, and integer overflow (use long if sums can exceed \( 2^{31} - 1 \)).

Java: Building the Prefix Array

int[] a = {2, 5, 1, 3, 7}; int n = a.length; // prefix array of size n + 1 long[] p = new long[n + 1]; // p[0] = 0 by default for (int i = 1; i <= n; i++) { p[i] = p[i - 1] + a[i - 1]; } // p = [0, 2, 7, 8, 11, 18]

Java: Querying a Range Sum

// Sum of \(a[L \ldots R]\) inclusive (0-indexed) static long rangeSum(long[] p, int L, int R) { return p[R + 1] - p[L]; } // Example: sum of a[1..3] long result = rangeSum(p, 1, 3); // p[4] - p[1] = 11 - 2 = 9 // Check: 5 + 1 + 3 = 9 ✓

Example 1: Simple Range Query

Problem: Given \( a = [4, 1, 6, 2, 5] \), find the sum of \( a[0 \ldots 2] \).

1
Build prefix array: \( p = [0, 4, 5, 11, 13, 18] \)
2
Apply formula: \( \text{sum}(0, 2) = p[3] - p[0] = 11 - 0 = \) \(11\)
3
Verify: \( 4 + 1 + 6 = 11 \) ✓

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

int[] a = {3, -2, 7, 1, -4, 5}; int n = a.length; long[] p = new long[n + 1]; for (int i = 1; i <= n; i++) p[i] = p[i-1] + a[i-1]; int[][] queries = {{1,3}, {0,5}, {4,4}}; for (int[] q : queries) { long ans = p[q[1]+1] - p[q[0]]; System.out.println(ans); } // Output: 6, 10, -4

Common Mistakes

✕ Off-by-One
Writing p[R] - p[L] instead of p[R+1] - p[L]. This misses \( a[R] \).
✕ Wrong Size
Allocating new long[n] instead of new long[n+1]. You need that extra slot for \( p[n] \).
✕ Integer Overflow
Using 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[]?

Question 5 of 5

What does the code print?