Running Sum of 1d Array (LeetCode 1480): Solution and Explanation

Easy
Array
Prefix Sum

Return an array where position i holds nums[0] + nums[1] + ... + nums[i], the total of everything up to and including index i.

Intuition

Read each number as a step: a positive value climbs and a negative value drops. The running sum at index i is simply the height you are standing at after taking steps 0 through i.

Recomputing every prefix from scratch would add the same numbers again and again, which is O(n^2). But the height after step i is just the height after step i - 1 plus nums[i], so each answer reuses the one before it.

Once nums[i - 1] holds its running sum, its original value is never needed again. That means you can overwrite the array in place: nums[i] += nums[i - 1], moving left to right.

Approach: Carry the total forward in place

  1. Leave nums[0] alone; the running sum of one element is the element itself.
  2. For i from 1 to n - 1, set nums[i] = nums[i] + nums[i - 1].
  3. Return nums.

Step-by-step example

nums = [3,1,2,10,1]
  1. Index 0 stays 3.
  2. i = 1: 1 + 3 = 4. nums = [3,4,2,10,1].
  3. i = 2: 2 + 4 = 6. nums = [3,4,6,10,1].
  4. i = 3: 10 + 6 = 16. nums = [3,4,6,16,1].
  5. i = 4: 1 + 16 = 17. The answer is [3,4,6,16,17].

Code: Python, Java, C++

1class Solution:
2    def runningSum(self, nums: List[int]) -> List[int]:
3        for i in range(1, len(nums)):
4            nums[i] += nums[i - 1]
5        return nums
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

Each index is visited once and does one addition.

Space

O(1)

The input array is reused as the output, so only the loop index is extra.

Other approaches

Separate output with a running total (time O(n), space O(n))

Keep a variable total, add each nums[i] to it and append it to a new list. Useful when the caller must not see its input modified.

Recompute every prefix (time O(n^2), space O(1))

For each i, sum nums[0..i] from scratch. Correct but repeats work for every position.

Common mistakes

  • Starting the loop at index 0 and reading nums[-1]. In Python that silently wraps to the last element; in other languages it is out of bounds.
  • Looping right to left. The update depends on the left neighbor already holding its running sum, so the pass must go left to right.
  • Forgetting that values can be negative. The running sum can go down and below zero; nothing about the method changes, but do not assume the output is increasing.

Frequently asked questions

Is this the same thing as a prefix sum array?

Yes. A running sum is the inclusive prefix sum. Many range-sum problems build this array first so that the sum of nums[l..r] becomes prefix[r] - prefix[l - 1].

Is modifying the input acceptable?

On LeetCode it is, and it gives O(1) extra space. If the caller still needs the original values, copy the array first or build a separate output.

Can the totals overflow?

Not under these constraints. At most 1000 values of magnitude at most 10^6 give a total of at most 10^9, which fits in a 32 bit integer.