Return the running sum of a one-dimensional array, where each position contains the sum of all elements up to that index.
Running Sum of a 1D Array
Given an integer array nums, build a new array runningSum such that runningSum[i] is the sum of the elements from index 0 through index i in nums.
In other words, each entry in the result should represent the cumulative total seen so far as you scan the array from left to right.
Input Format
- An integer array
numsof lengthn.
Output Format
- Return an integer array
runningSumof the same length, whererunningSum[i] = nums[0] + nums[1] + ... + nums[i].
Constraints
- .
- The array length is moderate enough for a linear scan solution.
- Elements may be positive, negative, or zero.
Example 1
Input
nums = [1,2,3,4]
Output
[1,3,6,10]
Explanation
The cumulative sums are 1, 1+2=3, 1+2+3=6, and 1+2+3+4=10.
Example 2
Input
nums = [1,1,1,1,1]
Output
[1,2,3,4,5]
Explanation
Each position adds one more element to the running total.
Show 1 more example
Example 3
Input
nums = [3,1,2,10,1]
Output
[3,4,6,16,17]
Explanation
Keep adding the current value to the total seen so far.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.