Community Problem
Running Sum of 1d Array
Difficulty: Easy
Build the prefix-sum array in place: each output index holds the cumulative sum of nums up to that index.
Running Sum of 1d Array
Build the prefix-sum array in place: each output index holds the cumulative sum of nums up to that index.
By @leoeriksson
December 21, 2025
·
Updated May 20, 2026
645 views
2
Rate
I keep this one on my warmup list when an intern asks how prefix-sum actually works in code, because the entire pattern fits in a three-line loop and the answer doubles as the data structure half the harder array problems need. The official practice catalog covers Subarray Sum Equals K and Range Sum Query, but it skipped the gentle, no-tricks introduction that every prefix-sum chain ultimately reduces to.
Running Sum of 1d Array
Given an integer array nums, return an array result of the same length where result[i] = nums[0] + nums[1] + ... + nums[i]. The returned array is the running sum (also called the prefix-sum) of nums.
Examples
Example 1:
- Input:
nums = [1, 2, 3, 4] - Output:
[1, 3, 6, 10] - Explanation: Running sum is
[1, 1+2, 1+2+3, 1+2+3+4].
Example 2:
- Input:
nums = [1, 1, 1, 1, 1] - Output:
[1, 2, 3, 4, 5] - Explanation: Each step adds another 1 to the running total.
Example 3:
- Input:
nums = [3, 1, 2, 10, 1] - Output:
[3, 4, 6, 16, 17] - Explanation: Running sum after each index in
nums.
Example 4:
- Input:
nums = [-1, 2, -3, 4] - Output:
[-1, 1, -2, 2] - Explanation: Running sums can decrease when negative values appear.
Constraints
1 <= nums.length <= 1000-10^6 <= nums[i] <= 10^6
Follow-up
Can you do this in O(1) extra space, mutating nums in place rather than allocating a new array?
Solution
Hints
Solution Walkthrough
Approach: One Pass with a Running Total (O(n) time, O(n) output / O(1) extra)
The definition of the running sum is recursive: result[i] = result[i - 1] + nums[i], with result[0] = nums[0]. That recursion turns into a single left-to-right loop that carries one accumulator. There is nothing fancy here, and that is exactly why the pattern is so reusable: every harder prefix-sum problem reduces to this same scan plus a derived check.
Algorithm
- Allocate an output array
resultof the same length asnums. - Initialize
total = 0. - For each index
ifrom0ton - 1:- Add
nums[i]tototal. - Write
totaltoresult[i].
- Return
result.
Why It Works
By induction. After step i, total equals nums[0] + nums[1] + ... + nums[i], which is the definition of result[i]. The base case i = 0 is total = 0 + nums[0] = nums[0], which matches result[0]. The inductive step i -> i + 1 adds nums[i + 1] to a value that already equals the prefix sum at i, producing the prefix sum at i + 1.
In-Place Variant (Follow-up)
If you are allowed to mutate nums, you can drop the result array and write the cumulative total back into nums[i] itself. The relation nums[i] = nums[i - 1] + nums[i] works because at the moment you read nums[i - 1] it has already been overwritten with the prefix sum, and nums[i] has not been touched yet.
export function runningSumInPlace(nums) {
for (let i = 1; i < nums.length; i++) {
nums[i] += nums[i - 1];
}
return nums;
}from __future__ import annotations
def running_sum_in_place(nums: list[int]) -> list[int]:
for i in range(1, len(nums)):
nums[i] += nums[i - 1]
return numsComplexity
Metric Value
Time O(n), one pass over nums
Space O(n) for the output array, or O(1) extra for the in-place variantPitfalls
- Forgetting the first index. If you start the loop at
i = 1without first writingresult[0] = nums[0], you will leaveresult[0]undefined (JavaScript) or zero (Python). - Reading the unwritten value in place. The in-place variant reads
nums[i - 1](already updated) andnums[i](still original). Reversing that order corrupts the running total. - Negative values. The running sum is not monotonic when
numscontains negatives, so do not assume the output is sorted.
Solution Code
