Running Sum of 1d Array (LeetCode 1480): Solution and Explanation
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
- Leave
nums[0]alone; the running sum of one element is the element itself. - For
ifrom 1 ton - 1, setnums[i] = nums[i] + nums[i - 1]. - Return
nums.
Step-by-step example
nums = [3,1,2,10,1]
- Index 0 stays 3.
- i = 1: 1 + 3 = 4. nums = [3,4,2,10,1].
- i = 2: 2 + 4 = 6. nums = [3,4,6,10,1].
- i = 3: 10 + 6 = 16. nums = [3,4,6,16,1].
- 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 nums1class Solution {
2 public int[] runningSum(int[] nums) {
3 for (int i = 1; i < nums.length; i++) {
4 nums[i] += nums[i - 1];
5 }
6 return nums;
7 }
8}1class Solution {
2public:
3 vector<int> runningSum(vector<int>& nums) {
4 for (int i = 1; i < nums.size(); i++) {
5 nums[i] += nums[i - 1];
6 }
7 return nums;
8 }
9};Time and space complexity
O(n)
Each index is visited once and does one addition.
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.