Cumulative Sum of an Array
Implement cumulativeSum
Given an array of integers
nums, return an array result where result[i] is the sum of nums[0..i] (inclusive) — the running total up to and including that index.
Recomputing each of these totals from scratch works, but it throws away everything the previous total already knew: the sum through index i is just the sum through index i - 1 plus nums[i]. Carrying a single running total forward — the prefix sumPrefix SumA running total built up left to right, where each position holds the sum of every element up through that point — the foundation for answering range-sum questions in O(1) after one O(n) pass. technique — turns an O(n²) scan into a single O(n) pass.
Example 1:
Input: nums = [1,2,3,4]
Output: [1,3,6,10]
Example 2:
Input: nums = [1,1,1,1,1]
Output: [1,2,3,4,5]
Example 3:
Input: nums = [-2,3,-1,5]
Output: [-2,1,0,5]
+ 7 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ nums.length ≤ 10⁵ - ●
-10⁴ ≤ nums[i] ≤ 10⁴
nums =
[1, 2, 3, 4]