Interview patterns

Prefix Sum Pattern Explained: Any Range Sum in One Subtraction

Updated Nov 27, 2026 · 3 min read

A prefix sum is a running total that starts at 0, so prefix[i] is the sum of the first i numbers. Build it once in O(n), and the sum of any inclusive range i to j is prefix[j + 1] minus prefix[i], one subtraction in O(1). It solves LeetCode 303, 560, 724 and 525.

Watch onYouTubeInstagram

Key points

  • ●Start the prefix list at 0, then add each number to the last total.
  • ●Range sum i..j inclusive = prefix[j + 1] - prefix[i].
  • ●O(n) to build once, then O(1) per query instead of O(n).
  • ●For subarray sum equals k, count prefix sums in a dict and add count[current - k].
Step-by-step visual sheet: Prefix Sum Pattern
The full visual sheet, made with Edithly.Open the interactive sheet

The clue: many sums on one list

You get a list that never changes and 1,000 questions, each one "what's the sum from index i to index j?" Re-adding the range every time costs O(n) per question. With a prefix sum, every answer is one subtraction.

Spot this pattern when a problem says:

  • "sum of elements between i and j", asked many times
  • "number of subarrays whose sum equals k"
  • "pivot index where the left sum equals the right sum"

The idea: a running total with a 0 in front

Walk the list once and write down the total so far. Start with 0, and each step adds the next number.

Index 0 1 2 3 4 5
nums 3 1 4 1 5
prefix 0 3 4 8 9 14

prefix[k] is the sum of the first k numbers. So the sum from i to j is everything up to j, minus everything before i:

range_sum(i, j) = prefix[j + 1] - prefix[i]

Trace one query

Sum of index 1 to 3, inclusive:

  • prefix[4] = 9 (that's 3 + 1 + 4 + 1)
  • prefix[1] = 3 (that's just the 3)
  • 9 - 3 = 6

Check it directly: 1 + 4 + 1 = 6. The visual sheet above shows the same list, the prefix row and this subtraction side by side.

The code in Python

prefix = [0]
for x in nums:
    prefix.append(prefix[-1] + x)

def range_sum(i, j):  # inclusive i..j
    return prefix[j + 1] - prefix[i]

With nums = [3, 1, 4, 1, 5], prefix becomes [0, 3, 4, 8, 9, 14], and range_sum(1, 3) returns 9 - 3 = 6.

The twist: subarray sum equals k

LeetCode 560 asks how many subarrays add up to k. A subarray ending at the current spot sums to k exactly when some earlier prefix sum equals current - k. So keep a dict that counts every prefix sum you've seen, and at each step add count[current - k].

def subarray_sum(nums, k):
    count = {0: 1}  # the empty prefix
    current = ans = 0
    for x in nums:
        current += x
        ans += count.get(current - k, 0)
        count[current] = count.get(current, 0) + 1
    return ans

For [1, 1, 1] and k = 2 it returns 2: the first two ones and the last two.

Complexity

Approach Preprocessing Per query
Without prefix sums none O(n), re-add the range
With prefix sums O(n) once O(1)

The O(1) only counts after you've paid the O(n) to build the list once. For a single query, just add the numbers. For 1,000 queries on 1,000 numbers, it's the difference between up to a million additions and about two thousand steps.

The trap

The formula is for an inclusive range, so it's prefix[j + 1], not prefix[j]. Using prefix[j] drops the last number. That off-by-one is the bug the 0 at the front sets up, and it's the most common one with this pattern.

Quick check: if a prefix list is 0, 2, 7, 9, the original numbers are 2, 5, 2. The second number is 7 - 2 = 5.

Where it shows up

  • 303 Range Sum Query
  • 560 Subarray Sum Equals K
  • 724 Find Pivot Index
  • 525 Contiguous Array
  • 238 Product of Array Except Self, with prefix products instead of sums

Frequently asked questions

What is a prefix sum?

A list of running totals with a 0 in front. For [3, 1, 4, 1, 5] it's [0, 3, 4, 8, 9, 14]. Entry i holds the sum of the first i numbers.

Why does the prefix sum list start with 0?

So every range has the same formula. The sum from index 0 to j is prefix[j + 1] - prefix[0], and prefix[0] = 0 makes that just prefix[j + 1], with no special case.

What is the time complexity of prefix sum?

O(n) once to build the running totals, then O(1) for each range-sum query. Without it, each query re-adds the range and costs O(n).

How does prefix sum solve Subarray Sum Equals K?

A subarray ending here sums to k when some earlier prefix equals current - k. Keep a dict counting how many times each prefix sum has appeared, and at each step add count[current - k] to the answer.

Turn any problem or document into a visual sheet like this one

Paste a LeetCode problem, a PDF or a link into Edithly and get a step-by-step sheet you can study, share or quiz yourself on.

Make this sheet in Edithly, free