Skip to content
IT glossary
AlgorithmsUpdated December 3, 2026

Prefix sums

Префиксные суммы

By storing the total of the first 0, 1, 2, and so on elements of an array, you can find the sum of any contiguous range with one subtraction. Those running totals are called prefix sums.

The idea

Suppose an array stores daily sales and you need totals for many date ranges. Instead of adding the daily figures again for each request, keep a running total from the start. To get sales for days 4 through 7, subtract the total through day 3 from the total through day 7. Both totals include days 1 through 3, so those days cancel out.

Build once, query by subtraction

For a zero-indexed array a, define prefix[i] as the sum of the first i elements. That means prefix[0] = 0 and prefix[i + 1] = prefix[i] + a[i]. The table has n + 1 entries for n input values. The leading zero represents the empty prefix, so ranges starting at index 0 use the same formula as every other range.

For a half-open range [left, right), which includes left but excludes right, the sum is prefix[right] - prefix[left]. If the problem gives an inclusive right endpoint, use prefix[right + 1] - prefix[left] instead.

sales = [10, 4, 8, 3]
prefix = [0]
for amount in sales:
    prefix.append(prefix[-1] + amount)

# prefix = [0, 10, 14, 22, 25]
left, right = 1, 3
print(prefix[right] - prefix[left])  # 12: 4 + 8

Building the table takes O(n) time and O(n) extra space. Once it exists, each query takes O(1) time no matter how long the range is. For q queries, that gives O(n + q) total time instead of the O(nq) worst case for summing each range from scratch.

When it fits, and what to check

Prefix sums work well when the input stays fixed and queries overlap or arrive in arbitrary order. They also make range averages cheap: get the range sum and divide by the number of elements in a nonempty range. If you only need one short range, just sum it directly. If individual values change frequently, a plain prefix table is a poor fit: an update near the start can invalidate almost the whole table. A Fenwick tree or segment tree can support both point updates and range sums in O(log n) time.

Most implementation bugs come from mixing inclusive and exclusive endpoints. Test a single-element range, a range starting at zero, and one ending at the last element. Negative numbers work fine for ordinary range sums, but the table may no longer be sorted, so don't assume you can binary-search it. In a language with fixed-width integers, choose a type that can hold the accumulated totals, not just the individual inputs.

A common interview follow-up

You may be asked to count contiguous subarrays whose sum is exactly k, without being given their endpoints. For each ending boundary right, rearrange the range formula: a valid starting boundary must have prefix[left] = prefix[right] - k. As you scan, use a hash table to count how often each earlier total occurred. Start with one occurrence of zero for the empty prefix, and look up the needed total before recording the current one. That ordering keeps you from counting an empty subarray when k is zero, and using frequencies lets you count multiple valid starts.