Inclusive prefix sum
P[i] includes a[i]: [3, 4, 8, 9, …]. NumPy's cumsum and C++ std::inclusive_scan return this.
Prefix sum array, O(1) range-sum queries and 2D prefix sums for a matrix.
Enter your values below to get started.
Leave the queries empty to get the prefix array only.
Switch to 0-based to match array indexes in Python, C, Java or JavaScript.
In 2D mode, each query is the top-left and bottom-right corners: r1 c1 r2 c2.
The Table tab lists every a[i] next to its prefix P[i].
A prefix sum calculator builds the running-total array P, where P[i] is the sum of the first i elements, and uses it to answer any range sum with a single subtraction. Enter a list (or a matrix for 2D prefix sums) and optional queries such as 2 5 to get the prefix array, each range sum, the working, a value table and a chart.
A prefix sum is the running total of an array: P[i] = a[1] + a[2] + … + a[i], so each entry holds the sum of everything up to that position. The array [3, 1, 4, 1, 5, 9, 2, 6] has the prefix sums [3, 4, 8, 9, 14, 23, 25, 31].
Prefix sums are called cumulative sums in statistics and scans (inclusive scan) in parallel computing. Guy Blelloch's 1990 work on scan algorithms made the prefix sum a building block of parallel programming on GPUs.
The prefix sum calculator makes 1 pass over the array to build P, then answers each range query [L, R] with 1 subtraction: P[R] − P[L − 1]. Building costs n additions; every query afterwards costs O(1), however long the range.
Read the values (negatives and decimals allowed) and the queries, using 1-based or 0-based indexes.
P[i] = P[i − 1] + a[i], starting from P[0] = 0.
sum[L..R] = P[R] − P[L − 1]. For [2, 5]: 14 − 3 = 11.
A matrix gets a 2D prefix table, and each rectangle sum takes 4 lookups.
The prefix sum formula is P[i] = P[i − 1] + a[i] with P[0] = 0, and the range sum formula is sum(L, R) = P[R] − P[L − 1]. The subtraction removes the part of the running total that comes before the range.
For [3, 1, 4, 1, 5, 9, 2, 6]: sum(3, 8) = P[8] − P[2] = 31 − 4 = 27, and 4 + 1 + 5 + 9 + 2 + 6 = 27.
There are 5 common types of prefix sums: the inclusive scan, the exclusive scan, the suffix sum, the 2D prefix sum and its inverse, the difference array. All 5 come from the same running-total idea.
P[i] includes a[i]: [3, 4, 8, 9, …]. NumPy's cumsum and C++ std::inclusive_scan return this.
E[i] stops before a[i]: [0, 3, 4, 8, …]. Used to compute output offsets in parallel algorithms.
S[i] = a[i] + … + a[n], a running total from the right end.
P[i][j] = sum of the rectangle from (1, 1) to (i, j). Called a summed-area table in image processing.
D[i] = a[i] − a[i − 1]. The prefix sum of D rebuilds a, so range updates become 2 point updates.
To calculate a prefix sum by hand, write the first element, then add each next element to the last total you wrote. For [3, 1, 4, 1, 5, 9, 2, 6]:
A 2D prefix sum stores, for every cell (i, j), the sum of the rectangle from the top-left corner to (i, j), and any sub-rectangle sum then takes 4 lookups by inclusion–exclusion.
Frank Crow introduced the summed-area table for texture mapping in 1984, and Paul Viola and Michael Jones used it in 2001 (as the integral image) to evaluate face-detection features in constant time. A 1920 × 1080 image has 2,073,600 pixels, yet any box sum costs 4 reads.
To use the prefix sum calculator, choose 1D or 2D, paste the values, add range queries if you need them, pick the index base and press Calculate. The result card shows the prefix array (or the first rectangle sum); the tabs show the steps, a chart or heat map, and the full table.
A list of numbers, or a matrix with one row per line.
Negatives and decimals are fine; up to 100,000 values or 10,000 cells.
1D: L R per line. 2D: r1 c1 r2 c2 per line. Up to 50 queries.
1-based for math notation, 0-based for code.
Prefix sums turn repeated range totals into single subtractions. There are 5 common uses:
Prefix arrays and range sums (1-based unless noted).
| Input | Query | Answer | Calculator |
|---|---|---|---|
| [3, 1, 4, 1, 5, 9, 2, 6] | Prefix array | [3, 4, 8, 9, 14, 23, 25, 31] | |
| [3, 1, 4, 1, 5, 9, 2, 6] | sum(2, 5) | 14 − 3 = 11 | |
| [3, 1, 4, 1, 5, 9, 2, 6] | sum(3, 8) | 31 − 4 = 27 | |
| [5, −2, 7, −1, 3] | sum(2, 4) | 9 − 5 = 4 | |
| [10, 20, 30, 40] (0-based) | sum(1, 2) | 60 − 10 = 50 | |
| 3 × 3 matrix 1…9 | (2, 2)–(3, 3) | 45 − 6 − 12 + 1 = 28 | |
| 3 × 3 matrix 1…9 | (1, 1)–(2, 3) | P[2][3] = 21 |
Try each one, then reveal the answer or solve it in the calculator.
Find the prefix sum array of [2, 4, 6, 8, 10].
For [2, 4, 6, 8, 10], find the sum from index 2 to 4 using prefix sums.
For [−3, 5, −2, 8, −1], find the prefix sums and sum(2, 4).
For ten 1s, what is sum(3, 7)?
Find the 2D prefix matrix of [[1, 2], [3, 4]].
In the 4 × 4 matrix of 1 to 16 (row by row), find the sum of rows 2–3, columns 2–3.
Discover 67 free sum calculators across different categories. Find the right calculator for your needs.
Try a different name or choose another category.
A prefix sum calculator is an online tool that builds the running-total array of a list, where P[i] is the sum of the first i elements, and answers range-sum queries with P[R] − P[L − 1]. This prefix sum calculator handles 1D arrays and 2D matrices.
The prefix sum formula is P[i] = P[i − 1] + a[i] with P[0] = 0. The sum of a range from L to R is P[R] − P[L − 1].
Yes, a prefix sum is the same as a cumulative sum or running total. Programmers say prefix sum or scan, and statisticians say cumulative sum.
Building a prefix sum array takes O(n) time and O(n) memory, and each range query then takes O(1). q queries on n elements cost O(n + q) instead of O(n·q).
Yes, prefix sums handle negative numbers. The running total can go down: [5, −2, 7] has prefix sums [5, 3, 10].
A 2D prefix sum is a table where each cell holds the sum of the rectangle from the top-left corner to that cell. Any rectangle sum then needs 4 lookups: P[r2][c2] − P[r1−1][c2] − P[r2][c1−1] + P[r1−1][c1−1].
A prefix sum adds elements up to each position, and a difference array stores a[i] − a[i − 1]. The 2 operations undo each other: the prefix sum of a difference array gives back the original array.
No, a plain prefix sum array goes stale when an element changes and must be rebuilt in O(n). A Fenwick tree (binary indexed tree) supports both updates and prefix queries in O(log n).
A prefix sum stores the running total of an array so that any range sum becomes 1 subtraction, and the prefix sum calculator builds that array, or a 2D summed-area table, and answers your queries with the working shown. Use 1-based indexes for math homework, 0-based indexes for code, and the 2D mode for matrices and images.
Not sure which structure your problem needs? Answer the questions below.