01 / 20
Σ Σ Σ Σ

Prefix Sums
in Python

Learn a powerful technique to answer range-sum queries in constant time using precomputed cumulative sums.

Python Algorithms

The Range-Sum Problem

Given an array of numbers, you are asked to find the sum of elements between two indices many times. For example: "What is the sum from index \(i\) to index \(j\)?"

2 5 1 3 7 4 6 0 1 2 3 4 5 6
\(\text{sum}(2, 4) = 11\)

What Is a Prefix Sum?

A prefix sum array stores the cumulative sum of elements from the start up to each index. If the original array is \(a\), the prefix sum array \(P\) is defined as:

Notice that \(P\) has one more element than \(a\). The entry \(P[k]\) equals the sum of the first \(k\) elements of \(a\).

Building the Prefix Array

a = 2 5 1 3 7 4 6 0 1 2 3 4 5 6 P = 0 2 7 8 11 18 22 28 0 1 2 3 4 5 6 7 Each P[k] = sum of first k elements of a

The Core Formula

Once you have the prefix sum array \(P\), the sum of any subarray \(a[i \ldots j]\) (inclusive) is computed instantly:

This works because \(P[j{+}1]\) includes everything from \(a[0]\) to \(a[j]\), and subtracting \(P[i]\) removes the elements before index \(i\). Each query now costs \(O(1)\).

Build
\(O(n)\) one-time cost to compute \(P\)
Query
\(O(1)\) per range-sum lookup
Total
\(O(n + q)\) for \(q\) queries

Why Subtraction Works

\(P[5] = 18\)
2 5 1 3 7
\(P[2] = 7\)
2 5
\(\text{sum}(2, 4) = 11\)
1 3 7
\(P[5] - P[2] = 18 - 7 = 11\)

Naive Loop vs Prefix Sum

Naive Loop Prefix Sum
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)\)
Best When Very few queries Many queries

Python Implementation

def build_prefix(a): n = len(a) P = [0] * (n + 1) for i in range(n): P[i + 1] = P[i] + a[i] return P def range_sum(P, i, j): # Sum of a[i..j] inclusive return P[j + 1] - P[i]

Step-by-Step Approach

1
Read the input array \(a\) of length \(n\).
2
Build the prefix array \(P\) with \(n{+}1\) elements, where \(P[0] = 0\) and \(P[k] = P[k{-}1] + a[k{-}1]\).
3
For each query \((i, j)\), compute \(P[j{+}1] - P[i]\).
4
Return the result. No loops needed: just one subtraction per query.

Example 1: Basic Query

Given \(a = [2, 5, 1, 3, 7, 4, 6]\), find the sum from index 1 to 3 (inclusive).

1
Build prefix: \(P = [0, 2, 7, 8, 11, 18, 22, 28]\)
2
Apply formula: \(\text{sum}(1, 3) = P[4] - P[1] = 11 - 2 = \) \(9\)
3
Verify: \(a[1] + a[2] + a[3] = 5 + 1 + 3 = \) \(9\) ✓

Example 2: Multiple Queries

Same array \(a = [2, 5, 1, 3, 7, 4, 6]\) with \(P = [0, 2, 7, 8, 11, 18, 22, 28]\). Answer three queries:

# Query 1: sum(0, 6) — entire array P[7] - P[0] = 28 - 0 = 28 # Query 2: sum(4, 5) P[6] - P[4] = 22 - 11 = 11 # Query 3: sum(3, 3) — single element P[4] - P[3] = 11 - 8 = 3

Example 3: Full Program

a = [4, -2, 3, 1, 5] P = build_prefix(a) # P = [0, 4, 2, 5, 6, 11] queries = [(0, 2), (1, 4), (2, 3)] for i, j in queries: print(range_sum(P, i, j)) # Output: # 5 (4 + -2 + 3) # 7 (-2 + 3 + 1 + 5) # 4 (3 + 1)

Python's itertools.accumulate

from itertools import accumulate a = [2, 5, 1, 3, 7, 4, 6] P = [0] + list(accumulate(a)) # P = [0, 2, 7, 8, 11, 18, 22, 28] # Query: sum(2, 4) print(P[5] - P[2]) # 11

Common Mistakes

✕ Off-by-One Error
Using \(P[j] - P[i]\) instead of \(P[j{+}1] - P[i]\). This excludes \(a[j]\) from the sum.
✕ Forgetting P[0] = 0
Starting the prefix array at \(P[0] = a[0]\) instead of \(0\). This breaks the formula for queries starting at index 0.
✕ Wrong Array Size
Making \(P\) of length \(n\) instead of \(n{+}1\). You need that extra leading zero element.

Summary at a Glance

Concept Details
Input Array \(a\) of \(n\) numbers
Prefix Array \(P[k] = a[0] + a[1] + \cdots + a[k{-}1]\)
Array Length \(P\) has \(n{+}1\) elements, starting with 0
Query Formula \(\text{sum}(i, j) = P[j{+}1] - P[i]\)
Build Time \(O(n)\)
Query Time \(O(1)\)
Space \(O(n)\)

Question 1 of 5

Given \(a = [3, 1, 4, 1, 5]\), what is the prefix sum array \(P\)?

Question 2 of 5

Using \(P = [0, 3, 4, 8, 9, 14]\), what is the sum of \(a[1 \ldots 3]\)?

Question 3 of 5

What is the time complexity of answering \(q\) range-sum queries on an array of length \(n\) using prefix sums?

Question 4 of 5

What Python code correctly builds a prefix sum array from list a?

Question 5 of 5

If \(P = [0, 2, 7, 8, 11, 18, 22, 28]\), what is the value of the original element \(a[4]\)?