Prefix Sum Visualizer
Build a prefix sum array step by step, choose a range, and inspect how two prefix lookups produce the final sum.
Array input
Step controls
Step 1 of 8: Start with prefix[0] = 0.
Prefix sum diagram
Start with prefix[0] = 0.
Range query
- Left
- 1
- Right
- 4
- prefix[right + 1]
- 7
- prefix[left]
- 2
Result
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.