Prefix Sums
Range Queries in C++
Learn how to precompute cumulative sums to answer range-sum queries in \( O(1) \) time after \( O(n) \) preprocessing.
The Range Sum Problem
Given an array of \( n \) integers and many queries, each asking: "What is the sum of elements from index \( l \) to index \( r \)?"
What Is a Prefix Sum?
A prefix sum array \( P \) stores the running total of all elements from the start up to each index. Formally:
Think of it as a cumulative total: each entry in \( P \) is "the sum of everything before this point." This lets us decompose any range sum into a simple subtraction.
Why Prefix Sums Matter
Prefix sums appear constantly in competitive programming, interviews, and real-world systems whenever you need fast repeated range queries on static data.
The Core Formula
\( P[r+1] \) is the sum of all elements up to index \( r \). Subtracting \( P[l] \) removes the part before the range, leaving exactly the elements from \( l \) to \( r \).
Visual Intuition
Brute Force vs Prefix Sums
| Brute Force | Prefix Sums | |
|---|---|---|
| Preprocessing | None | \( O(n) \) |
| Per Query | \( O(n) \) | \( O(1) \) |
| Total for \( q \) queries | \( O(n \cdot q) \) | \( O(n + q) \) |
| Extra Space | \( O(1) \) | \( O(n) \) |
| Handles Updates? | Yes (re-sum) | No (static only) |
How to Apply Prefix Sums
C++ Implementation
int n = a.size();
vector<long long> p(n + 1, 0);
for (int i = 1; i <= n; i++)
p[i] = p[i - 1] + a[i - 1];
return p;
}
// Query: sum of a[l..r] (0-indexed)
long long rangeSum(const vector<long long>& p,
int l, int r) {
return p[r + 1] - p[l];
}
Example 1: Building the Array
Given \( A = [2, 5, 1, 4, 3] \), build the prefix sum array \( P \).
Example 2: Answering Queries
Using \( A = [2, 5, 1, 4, 3] \) and \( P = [0, 2, 7, 8, 12, 15] \):
Example 3: Full Walkthrough
vector<int> a = {10, 20, 30, 40, 50};
auto p = buildPrefix(a);
// p = {0, 10, 30, 60, 100, 150}
cout << rangeSum(p, 1, 3); // 90
// P[4] - P[1] = 100 - 10 = 90
// Verify: 20 + 30 + 40 = 90 ✓
cout << rangeSum(p, 0, 0); // 10
// P[1] - P[0] = 10 - 0 = 10 ✓
}
The 1-Indexed Variant
Some problems (and many contest templates) use 1-based indexing. The formula changes slightly:
Query: \( P[r+1] - P[l] \)
Query: \( P[r] - P[l-1] \)
Common Mistakes
Summary Cheat Sheet
| Aspect | Detail |
|---|---|
| Build Time | \( O(n) \) |
| Query Time | \( O(1) \) |
| Space | \( O(n) \) extra for \( P \) |
| Array Size | \( n + 1 \) (one extra element) |
| Data Type | long long to prevent overflow |
| Limitation | Static arrays only (no updates) |
| Key Formula (0-idx) | \( P[r+1] - P[l] \) |
Question 1 of 5
Given \( A = [3, 1, 4, 1, 5] \), what is \( P[3] \) in the 0-indexed prefix sum array?
Question 2 of 5
Using 0-indexed prefix sums, the sum of elements from index \( l \) to \( r \) is:
Question 3 of 5
If \( P = [0, 5, 12, 18, 27, 33] \), what is sum(2, 4)?
Question 4 of 5
What is the time complexity to answer \( q \) range-sum queries on an array of size \( n \) using prefix sums?
Question 5 of 5
Which of the following is a limitation of the basic prefix sum technique?