AlgoMaster Logo

Running Sum of 1d Array

Last Updated: March 29, 2026

easy

Understanding the Problem

We're given an array and need to produce a new array where each element at index i is the sum of all elements from index 0 through i in the original array. This is also known as a prefix sum, one of the most fundamental building blocks in algorithm design.

The first element stays the same (it's just the sum of itself). The second element is the sum of the first two, the third is the sum of the first three, and so on. Notice that each running sum value builds on the previous one. If you already know the running sum up to index i - 1, the running sum at index i is just runningSum[i - 1] + nums[i]. That observation is the key to solving this efficiently.

Key Constraints:

  • 1 <= nums.length <= 1000 → With at most 1000 elements, even an O(n^2) approach runs comfortably under time limits. An O(n) solution is trivial to achieve.
  • -10^6 <= nums[i] <= 10^6 → Values can be negative. The running sum can grow as large as 10^9 in magnitude (1000 elements times 10^6 each), which fits within a 32-bit integer.

Approach 1: Brute Force (Recompute Each Sum)

Intuition

The most straightforward way: for each index i, sum up all elements from 0 to i. This directly follows the definition of running sum without trying to be clever about it. For each position, start a fresh summation from the beginning of the array.

This is what most people would write if they're just translating the problem statement into code without thinking about redundancy.

Algorithm

  1. Create a result array of the same length as nums.
  2. For each index i from 0 to n - 1:
    • Initialize a sum to 0.
    • For each index j from 0 to i, add nums[j] to the sum.
    • Set result[i] to this sum.
  3. Return the result array.

Example Walkthrough

Input:

0
1
1
2
2
3
3
4
nums

For each index, we sum all elements from 0 to i:

0
1
1
3
2
6
3
10
result

Code

This approach works but recomputes the same sums repeatedly. What if we built each running sum incrementally from the previous one instead of starting from scratch?

Approach 2: Prefix Sum (New Array)

Intuition

The brute force recomputes the same sums over and over. But there's a simple relationship staring at us: runningSum[i] = runningSum[i - 1] + nums[i]. Each running sum is just the previous running sum plus the current element.

So instead of nested loops, we can do a single pass. Keep a running total, add each element to it, and that's the answer for that position. This is the prefix sum technique, and it's one of those patterns that shows up constantly in more complex problems.

Algorithm

  1. Create a result array of the same length as nums.
  2. Set result[0] = nums[0] (the first element has no previous sum to build on).
  3. For each index i from 1 to n - 1:
    • Set result[i] = result[i - 1] + nums[i].
  4. Return the result array.

Example Walkthrough

1Start: result[0] = nums[0] = 3
0
3
i
1
1
2
2
3
10
4
1
1/6
1result[0] = 3
[3, _, _, _, _]
1/6

Code

This approach is optimal in time, but uses O(n) extra space for the result array. Can we compute the running sum in-place by modifying the input array directly?

Approach 3: In-Place Prefix Sum (Optimal)

Intuition

If modifying the input array is acceptable, we can skip creating a new array entirely. Just iterate from index 1 and add the previous element to the current one. After the loop, nums itself holds the running sum.

This is the cleanest version of the solution. One loop, one line of logic, no extra space.

Algorithm

  1. For each index i from 1 to n - 1:
    • Add nums[i - 1] to nums[i].
  2. Return nums.

Example Walkthrough

1Initial: nums = [1, 2, 3, 4], start from i=1
0
1
1
2
i
2
3
3
4
1/5

Code