629/670

629. Running Sum of 1d Array

Easy

You are given an array of integers nums. Build and return its running sum: a new array of the same length where position i holds the total of everything up to and including nums[i] — that is, runningSum[i] = nums[0] + nums[1] + … + nums[i].

The first entry is just nums[0]; every later entry is the previous running total plus the current number. This is the simplest form of a prefix sum, the building block behind constant-time range-sum queries.

Example 1:

Input: nums = [2, 5, 1, 3]

Output: [2, 7, 8, 11]

Explanation: The totals grow as 2, then 2+5 = 7, then 7+1 = 8, then 8+3 = 11.

Example 2:

Input: nums = [4, -1, -6, 2]

Output: [4, 3, -3, -1]

Explanation: Negative numbers pull the total down: 4, 4−1 = 3, 3−6 = −3, −3+2 = −1.

Constraints:

  • 1 ≤ nums.length ≤ 10⁴
  • -10⁶ ≤ nums[i] ≤ 10⁶

Hints:

Each answer differs from the one before it by exactly one term. Which term?

Carry one accumulator across a single pass: add nums[i] to it, and that IS runningSum[i].

▶ Run checks these sample cases. Submit also runs hidden edge cases.

Input: nums = [2, 5, 1, 3]

Expected output: [2, 7, 8, 11]