Prefix Sum Calculator

Prefix sum array, O(1) range-sum queries and 2D prefix sums for a matrix.

  1. Enter valuesUse your own inputs or an example
  2. CalculateApply your selected settings
  3. View resultExplore the answer and explanation

Enter the array and queries

Enter your values below to get started.

Examples:
Data
1D: values separated by spaces or commas. 2D: one matrix row per line.
1D: one query per line as "L R". 2D: "top left bottom right", e.g. 2 2 3 3.
Reset restores the default values.

Tips for this calculator

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.

What is a Prefix Sum?

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].

P[i]=∑k=1ia[k]P[i] = \sum_{k=1}^{i} a[k]

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.

InteractiveValues and their running total

How Does the Prefix Sum Calculator Work?

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.

InteractiveAny range sum = 2 prefix sums
  1. Parse

    Read the values (negatives and decimals allowed) and the queries, using 1-based or 0-based indexes.

  2. Build P

    P[i] = P[i − 1] + a[i], starting from P[0] = 0.

  3. Answer queries

    sum[L..R] = P[R] − P[L − 1]. For [2, 5]: 14 − 3 = 11.

  4. 2D mode

    A matrix gets a 2D prefix table, and each rectangle sum takes 4 lookups.

Prefix Sum Formula and Range Sum Formula

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.

P[i]=P[i−1]+a[i],∑k=LRa[k]=P[R]−P[L−1]P[i] = P[i-1] + a[i], \qquad \sum_{k=L}^{R} a[k] = P[R] - P[L-1]

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.

InteractiveBuild the prefix array 1 addition at a time

Types of Prefix Sums

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.

InteractiveSame array, 4 kinds of scan

Inclusive prefix sum

P[i] includes a[i]: [3, 4, 8, 9, …]. NumPy's cumsum and C++ std::inclusive_scan return this.

Exclusive prefix sum

E[i] stops before a[i]: [0, 3, 4, 8, …]. Used to compute output offsets in parallel algorithms.

Suffix sum

S[i] = a[i] + … + a[n], a running total from the right end.

2D prefix sum

P[i][j] = sum of the rectangle from (1, 1) to (i, j). Called a summed-area table in image processing.

Difference array

D[i] = a[i] − a[i − 1]. The prefix sum of D rebuilds a, so range updates become 2 point updates.

How to Calculate a Prefix Sum by Hand

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]:

  1. P[1] = 3.
  2. P[2] = 3 + 1 = 4; P[3] = 4 + 4 = 8; P[4] = 8 + 1 = 9.
  3. P[5] = 9 + 5 = 14; P[6] = 14 + 9 = 23.
  4. P[7] = 23 + 2 = 25; P[8] = 25 + 6 = 31.
  5. Range 2 to 5: P[5] − P[1] = 14 − 3 = 11.
InteractiveStep through prefix sums by hand

    2D Prefix Sums and Rectangle Queries

    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.

    S=P[r2][c2]−P[r1−1][c2]−P[r2][c1−1]+P[r1−1][c1−1]S = P[r_2][c_2] - P[r_1-1][c_2] - P[r_2][c_1-1] + P[r_1-1][c_1-1]

    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.

    InteractiveRectangle sum with 4 lookups

    Common Mistakes with Prefix Sums

    • Off-by-one ranges: sum(L, R) uses P[L − 1], not P[L]; P[R] − P[L] drops a[L].
    • Mixing 0-based and 1-based indexes: with 0-based arrays, sum(L, R) = P[R] − P[L − 1] needs P[−1] = 0; an exclusive scan with a leading 0 avoids that case.
    • Forgetting the + corner in 2D: the top-left block is subtracted twice, so add P[r1 − 1][c1 − 1] back.
    • Integer overflow: prefix sums grow; 100,000 values of 1,000,000 reach 1011, beyond a 32-bit int.
    • Updating a without rebuilding P: a changed element makes every later prefix wrong; use a Fenwick tree for frequent updates.
    InteractiveTrue or false: prefix sums
    1. sum(L, R) = P[R] − P[L].

    2. After building P, each range sum takes the same time whatever the range length.

    3. The prefix sums of [5, −2, 7] are [5, 3, 10].

    4. A 2D rectangle sum needs 4 prefix lookups.

    5. A difference array is the prefix sum of the prefix sum.

    Score: 0 / 5

    How to Use the Prefix Sum Calculator

    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.

    InteractiveLoad a prefix sum problem

    Each button fills the calculator and runs it.

    1. Pick 1D or 2D

      A list of numbers, or a matrix with one row per line.

    2. Paste the values

      Negatives and decimals are fine; up to 100,000 values or 10,000 cells.

    3. Add queries

      1D: L R per line. 2D: r1 c1 r2 c2 per line. Up to 50 queries.

    4. Pick the index base

      1-based for math notation, 0-based for code.

    Real-Life Uses of Prefix Sums

    Prefix sums turn repeated range totals into single subtractions. There are 5 common uses:

    • Sales and finance reports: revenue between any 2 dates comes from 2 entries of a cumulative revenue column.
    • Image processing: integral images give box-blur and Viola–Jones feature sums in 4 lookups per box.
    • Competitive programming: subarray-sum and equilibrium-index problems drop from O(n²) to O(n).
    • Parallel computing: GPU stream compaction and radix sort use exclusive scans to find output positions.
    • Geography and fitness: elevation gain over a stretch of a route equals the difference of 2 cumulative climb totals in meters (or feet).
    InteractiveSales between any 2 days of the month

    Prefix Sum Examples

    Prefix arrays and range sums (1-based unless noted).

    Prefix Sum Examples
    InputQueryAnswerCalculator
    [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

    Popular Prefix Sum Problems

    Try each one, then reveal the answer or solve it in the calculator.

    1. Find the prefix sum array of [2, 4, 6, 8, 10].

      Show answer[2, 6, 12, 20, 30]
    2. For [2, 4, 6, 8, 10], find the sum from index 2 to 4 using prefix sums.

      P[4] - P[1]
      Show answer20 - 2 = 18
    3. For [−3, 5, −2, 8, −1], find the prefix sums and sum(2, 4).

      Show answerP = [−3, 2, 0, 8, 7]; sum(2, 4) = 8 − (−3) = 11
    4. For ten 1s, what is sum(3, 7)?

      P[7] - P[2]
      Show answer7 - 2 = 5
    5. Find the 2D prefix matrix of [[1, 2], [3, 4]].

      Show answer[[1, 3], [4, 10]]
    6. In the 4 × 4 matrix of 1 to 16 (row by row), find the sum of rows 2–3, columns 2–3.

      6 + 7 + 10 + 11
      Show answer34

    Explore More Sum Calculators

    Discover 67 free sum calculators across different categories. Find the right calculator for your needs.

    Browse All 67 Calculators
    Questions and answers

    Frequently Asked Questions

    What is a prefix sum calculator?

    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.

    What is the prefix sum formula?

    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].

    Is a prefix sum the same as a cumulative sum?

    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.

    What is the time complexity of prefix sums?

    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).

    Can prefix sums handle negative numbers?

    Yes, prefix sums handle negative numbers. The running total can go down: [5, −2, 7] has prefix sums [5, 3, 10].

    What is a 2D prefix sum?

    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].

    What is the difference between a prefix sum and a difference array?

    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.

    Do prefix sums work when the array changes?

    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).

    Conclusion

    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.

    InteractivePrefix sum, difference array or Fenwick tree?