Prefix Sum Visualizer

Build a prefix sum array step by step, choose a range, and inspect how two prefix lookups produce the final sum.

Values
6
Prefix
7
Index
-
Sum
5

Array input

Step controls

Step 1 of 8: Start with prefix[0] = 0.

Prefix sum diagram

Start with prefix[0] = 0.

Query range Current Formula
Input array
0
2
1
-1
2
3
3
5
4
-2
5
4
Prefix array
0
0
1
-
2
-
3
-
4
-
5
-
6
-

Range query

Left
1
Right
4
prefix[right + 1]
7
prefix[left]
2

Result

range = [1, 4]
formula = prefix[5] - prefix[1]
sum = 5

What is a Prefix Sum Visualizer?

A prefix sum visualizer shows how cumulative sums are stored so range sum queries can be answered in constant time after preprocessing.

This tool builds the prefix array step by step. It then shows how a range query from left to right uses prefix[right + 1] - prefix[left].

For the full cluster of related tools, browse the Data Structure Visualizers hub.

How to use this prefix sum visualizer

  • Paste an array such as [2, -1, 3, 5, -2, 4].
  • Choose the left and right indexes for the range query.
  • Step through each cumulative-sum update.
  • Run to the end to see the final query formula.
  • Compare the highlighted array range with the matching prefix values.

Why prefix sums are useful

Without prefix sums, each range sum query needs to scan the selected values. With prefix sums, the array is scanned once during preprocessing, then each query can be answered with two prefix lookups.

Prefix sums are common in subarray problems, range sum queries, grid sums, and coding interview questions where repeated sum checks would otherwise be too slow.

If you need range queries with point updates, compare this tool with the Segment Tree Visualizer and Fenwick Tree Visualizer.

Compare this page with the Sliding Window Visualizer, Two Pointers Visualizer, and Matrix Traversal Visualizer.

Frequently Asked Questions

What input format does this prefix sum visualizer use?
Paste an array, comma-separated list, or space-separated list of numbers.
Why does the prefix array start with zero?
The leading zero lets the same formula work for ranges that start at index 0.
Can prefix sums include negative numbers?
Yes. Prefix sums work with positive, zero, and negative values because each prefix entry stores the cumulative total so far.
What is the range sum formula?
For an inclusive range from left to right, use prefix[right + 1] - prefix[left].

Related tools