Prefix Sums & Difference Arrays for Constant-Time Range Queries0%

Prefix Sums & Difference Arrays for Constant-Time Range Queries

Beginner12 min read•Updated: Jul 11, 2026
Study MaterialsCheatsheet

Prefix Sums & Difference Arrays for Constant-Time Range Queries

When dealing with frequent range sum queries or bulk range updates on an array, naive solutions require $O(n)$ per query, resulting in an unscalable $O(n \times q)$ for $q$ queries. Prefix Sum Arrays solve range sum queries in $O(1)$ constant time, while Difference Arrays apply range updates across intervals in $O(1)$ constant time.


1. Real-World Analogy: The Odometer & Ledger

Imagine tracking your car's fuel expenses:

  • Instead of writing down how much you spent on every 5 km interval and adding them up every time someone asks your expenses between day 10 and day 20, you record your running cumulative total at the end of every day.
  • To find spending between Day $L$ and Day $R$, you simply compute:

$$\text{Total}(L \to R) = \text{Odometer}(R) - \text{Odometer}(L - 1)$$ A single subtraction gives you the exact total instantly!


2. Prefix Sum Array Construction & Formula

Given an array arr: $$\text{prefix}[0] = \text{arr}[0]$$ $$\text{prefix}[i] = \text{prefix}[i - 1] + \text{arr}[i]$$

Range Sum Query Formula (Inclusive indices $L$ to $R$):

$$\text{Sum}(L, R) = \begin{cases} \text{prefix}[R] & \text{if } L = 0 \\ \text{prefix}[R] - \text{prefix}[L - 1] & \text{if } L > 0 \end{cases}$$

Output
Original: [3, 1, 4, 1, 5]
Prefix: [3, 4, 8, 9, 14]
 
Sum from index 1 to 3 (values: 1 + 4 + 1 = 6):
Formula: prefix[3] - prefix[0] = 9 - 3 = 6! (Calculated in O(1))

3. Difference Arrays: $O(1)$ Range Updates

Suppose you need to add $+V$ to all elements in range $[L, R]$ for 100,000 queries:

  • Naive loop: $O(R - L) \approx O(n)$ per update.
  • Difference Array Technique:
  1. 1
    $\text{diff}[L] \mathrel{+}= V$ (marks the start of the increment)
  2. 2
    $\text{diff}[R + 1] \mathrel{-}= V$ (cancels out the increment after the boundary!)
  3. 3
    After all $q$ queries, compute the prefix sum of the difference array in a single $O(n)$ pass!

4. Multi-Language Implementations: Prefix Sum Class

Python 3:

Python
class PrefixSum:
def __init__(self, nums: list[int]):
# 1-indexed prefix array simplifies boundary checks (prefix[0] = 0)
self.pref = [0] * (len(nums) + 1)
for i in range(len(nums)):
self.pref[i + 1] = self.pref[i] + nums[i]
 
def query_range(self, left: int, right: int) -> int:
# Sum of nums[left...right] in O(1) time
return self.pref[right + 1] - self.pref[left]

Modern JavaScript / TypeScript:

TypeScript
export class PrefixSum {
private pref: number[];
 
constructor(nums: number[]) {
this.pref = new Array(nums.length + 1).fill(0);
for (let i = 0; i < nums.length; i++) {
this.pref[i + 1] = this.pref[i] + nums[i];
}
}
 
public queryRange(left: number, right: number): number {
return this.pref[right + 1] - this.pref[left];
}
}

5. Trace Table: Range Updates via Difference Array

Initial Array of size 5: [0, 0, 0, 0, 0]

  • Query 1: Add $+10$ to range $[1, 3]$
  • Query 2: Add $+5$ to range $[2, 4]$
StepIndex 0Index 1Index 2Index 3Index 4Index 5
Initial diff000000
After Q1: diff[1]+=10, diff[4]-=100+1000-100
After Q2: diff[2]+=5, diff[5]-=50+10+50-10-5
Prefix Sum Pass (Final Values)010151550

Resulting array [0, 10, 15, 15, 5] generated with zero looping per query!


Interactive Quiz

Multiple Choice Questions

Question 1
What is the time complexity to answer a single range sum query sumRange(L, R) using a precomputed prefix sum array?
Question 2
How much auxiliary space does a standard 1D prefix sum array require for an input of size $n$?
Question 3
In a Difference Array, how do you mark adding a value $V$ to all indices in the inclusive interval $[L, R]$?
Question 4
What is the primary advantage of initializing a prefix sum array with size $n + 1$ and setting prefix[0] = 0?
Question 5
In 2D Matrix Prefix Sums, what is the formula to compute the submatrix sum from $(r_1, c_1)$ to $(r_2, c_2)$?

Next Lesson

Singly Linked Lists: Node Architecture, Traversal & In-Place Reversal

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

PrevNext