Running Sum of 1d Array
The total up to i is just the total up to i − 1 plus nums[i]. So walk once from left to right and add the previous total into the current cell. We can even store it back into nums, because we never need the old value again.
Before we handle index i, the cell nums[i − 1] already holds the full total of everything before i. That one fact is all we need.
Problem
You are given an integer array nums.
Build a new array where position i holds the total of everything from nums[0] up to and including nums[i]. This is called the running sum.
An integer array nums.
An array of the same length, where element i is nums[0] + nums[1] + … + nums[i].
1 = 1, 3 = 1+2, 6 = 1+2+3, 10 = 1+2+3+4.
Each new 1 raises the total by one.
3, then 3+1 = 4, then 4+2 = 6, then 6+10 = 16, then 16+1 = 17.
- • 1 ≤ nums.length ≤ 1000
- • −10^6 ≤ nums[i] ≤ 10^6
- • The biggest possible total is 1000 · 10^6 = 10^9, which still fits in an `int`.
First, think yourself
Before opening any solution, try to answer these on paper. Use hints only when stuck — each hint is counted.
- 1.
What is the answer at index 0? Do I need to add anything?
- 2.
Write the totals for index 2 and index 3 side by side. What do they share?
- 3.
If I already know the total up to index i − 1, how much extra work is the total up to i?
- 4.
Do I still need the original
nums[i]after I have used it once? - 5.
Is the answer at index i "up to i" or "before i"? Does that matter?
Brute force approach
Optimized approach
Why does this algorithm work?
Dry run
All approaches compared
| Approach | Idea | Time | Space |
|---|---|---|---|
| Brute Force | For each index, add up nums[0..i] from scratch | O(N²) | O(1) |
| Optimal | One pass: running[i] = running[i−1] + nums[i], done in place | O(N) | O(1) |
Why does this belong to this pattern?
- “sum of nums[0] up to nums[i]”A total that always starts at index 0 → a prefix (running) sum.
- “for every index”We need all the totals, so build them in one left-to-right pass.
- “each total includes the one before”total(i) = total(i − 1) + nums[i] → one addition per index.
This is the anchor of the Static range sum sub-pattern. Here we only build the running totals. The next two questions use the same totals to answer any range sum with one subtraction. To make that subtraction clean, they shift the array by one and put a 0 in front — see the first mistake below for how the two versions line up.
Don't make this mistake
There are two common versions. Running sum (this problem): same length as nums, and running[i] includes nums[i]. Prefix with a leading 0 (used for range queries): length n + 1, prefix[0] = 0, and prefix[i] is the sum of everything before i. So prefix[i + 1] = running[i]. Use the running sum when the question wants the totals themselves. Use the leading-0 version when you will subtract two totals to get a range: it never needs index −1. Most off-by-one bugs in this pattern come from using one version's formula on the other.
At i = 0 there is nothing before it, and nums[-1] reads outside the array. Index 0 is already its own total.
You must add the total so far, not just the previous element. The wrong version gives the sum of two neighbours, e.g. [1,2,3,4] → [1,3,5,7] instead of [1,3,6,10].
Pattern-confusion trap. A sliding window is for a range that moves (its left end goes forward). Here every total starts at index 0 — the left end never moves — so there is nothing to shrink. It is just a running total.
Want to implement this?
Write it yourself on LeetCode, then come back and compare your code with the solutions above.
Open coding workspaceHow this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Prefix Sum translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.