2Brute2Best
🔥0

Running Sum of 1d Array

Easy·LeetCode #1480Basic Anchor
LeetCode
30-Second Mental ModelPrefix Sum (running total)
Complexity:O(N²)→O(N)
Key Insight

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.

Core Validity Invariant

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.

01

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.

Input

An integer array nums.

Output

An array of the same length, where element i is nums[0] + nums[1] + … + nums[i].

Example 1
Input: nums = [1, 2, 3, 4]
Output: [1, 3, 6, 10]

1 = 1, 3 = 1+2, 6 = 1+2+3, 10 = 1+2+3+4.

Example 2
Input: nums = [1, 1, 1, 1, 1]
Output: [1, 2, 3, 4, 5]

Each new 1 raises the total by one.

Example 3
Input: nums = [3, 1, 2, 10, 1]
Output: [3, 4, 6, 16, 17]

3, then 3+1 = 4, then 4+2 = 6, then 6+10 = 16, then 16+1 = 17.

Constraints
  • • 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`.
02

First, think yourself

Before opening any solution, try to answer these on paper. Use hints only when stuck — each hint is counted.

  1. 1.

    What is the answer at index 0? Do I need to add anything?

  2. 2.

    Write the totals for index 2 and index 3 side by side. What do they share?

  3. 3.

    If I already know the total up to index i − 1, how much extra work is the total up to i?

  4. 4.

    Do I still need the original nums[i] after I have used it once?

  5. 5.

    Is the answer at index i "up to i" or "before i"? Does that matter?

03

Brute force approach

05

Optimized approach

06

Why does this algorithm work?

07

Dry run

08

All approaches compared

ApproachIdeaTimeSpace
Brute ForceFor each index, add up nums[0..i] from scratchO(N²)O(1)
OptimalOne pass: running[i] = running[i−1] + nums[i], done in placeO(N)O(1)
09

Why does this belong to this pattern?

Clues in the problem statement
  • “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.
Total from the start+needed at every index+each total extends the previous one→Prefix Sum (running total)

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.

10

Don't make this mistake

Mixing up the running sum with the "prefix with a leading 0"
✗ // running[i] = nums[0..i] (this problem) // then using it for a range query: sum(l, r) = running[r] - running[l - 1]; // crashes when l = 0
✓ // prefix has size n + 1 and prefix[0] = 0 // prefix[i] = nums[0..i-1] (sum BEFORE index i) sum(l, r) = prefix[r + 1] - prefix[l];

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.

Starting the loop at i = 0
✗ for (int i = 0; i < n; i++) nums[i] += nums[i - 1];
✓ for (int i = 1; i < n; i++) nums[i] += nums[i - 1];

At i = 0 there is nothing before it, and nums[-1] reads outside the array. Index 0 is already its own total.

Adding the original value instead of the running total
✗ result[i] = nums[i - 1] + nums[i]; // nums is never updated
✓ result[i] = result[i - 1] + nums[i];

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

Reaching for a sliding window

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.

11

Want to implement this?

Write it yourself on LeetCode, then come back and compare your code with the solutions above.

Open coding workspace
How did it go?
Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Prefix Sum translate into production architectures and low-level memory mechanics.