Skip to content

Practice Running Sum of 1d Array

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Easy#1480
LeetCode Problem

Running Sum of 1d Array

Problem Statement

Given an array nums. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]). Return the running sum of nums.

Source: Running Sum of 1d Array on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

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

Carry a total while moving left to right: 1, then 1 + 2 = 3, then 3 + 3 = 6, then 6 + 4 = 10. Store the total after adding the current value, producing [1, 3, 6, 10]. Reusing the previous total visits each element once, instead of adding the full prefix again for every output position.

# Example 2

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

Each successive 1 increases the cumulative total by one, giving [1, 2, 3, 4, 5]. There is one output per input element, including the first prefix of length one. An implementation that stores the total before adding the current element would shift the results and incorrectly begin with zero.

# Example 3

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

The cumulative totals are 3, 3 + 1 = 4, 4 + 2 = 6, 6 + 10 = 16, and 16 + 1 = 17. In particular, the value 10 is added to the accumulated 6, not just to its immediate predecessor 2. Keep the running total across iterations; restarting it would produce adjacent-pair sums rather than prefix sums.

Constraints

1 <= nums.length <= 1000
-10^6 <= nums[i] <= 10^6

How to solve Running Sum of 1d Array

Original notes for interview preparation, written for this problem. Reviewed .

Key insight

Each running sum equals the previous running sum plus the current element, so a single accumulator produces the whole output in one left-to-right pass.

Why brute force is not enough

Recomputing each prefix from scratch sums i elements for position i, giving O(n^2) additions; with nums.length up to 1000 that is roughly 500,000 operations, which runs but wastes effort and signals that you did not notice the overlap between consecutive prefixes.

Approach

  1. Keep a running total initialized to zero and an output array the same length as nums.
  2. Move left to right; add the current element to the total before storing it, so position i holds the sum of elements 0 through i inclusive.
  3. The invariant is that after processing index i the total equals the sum of the first i + 1 elements, so nothing earlier is ever revisited.
  4. Optionally write the totals back into nums itself to avoid the extra array when mutating the input is acceptable.

Walkthrough of Example 3

nums = [3,1,2,10,1] → [3,4,6,16,17]

  1. Start with total 0. Add 3: total 3, output so far [3].
  2. Add 1: total 4, output [3, 4].
  3. Add 2: total 6, output [3, 4, 6].
  4. Add 10: total 16, output [3, 4, 6, 16].
  5. Add 1: total 17, final output [3, 4, 6, 16, 17].

Time and space

Time O(n) · Space O(1). n is the array length; one addition per element, and only the accumulator is extra when the result is written in place (the output itself is O(n) if counted).

Interview pitfalls

  • Storing the total before adding the current element, which shifts every value and makes the output start with 0.
  • Resetting the accumulator inside the loop, which produces adjacent-pair sums instead of prefix sums.
  • Overflow awareness: magnitudes up to 10^6 across 1000 elements bound the running sum by 10^9 in absolute value, which fits a signed 32-bit integer only barely; a variant with larger bounds needs 64-bit arithmetic.
  • Assuming the running sum is monotone; negative elements make it rise and fall, which matters for follow-up questions.

Follow-up questions interviewers ask

  • Given the running sums, how do you answer many range-sum queries in O(1) each, and how do you handle a left boundary of 0 without a special case?
  • How would you count subarrays whose sum equals k in one pass using prefix sums and a hash map?
  • How does the idea extend to a two-dimensional prefix sum that answers rectangle-sum queries on a matrix?

Further reading on the technique: Prefix sum

Related LeetCode Problems

Practice more problems similar to Running Sum of 1d Array.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.