Given an array nums, we need to compute the running sum (also known as cumulative sum or prefix sum) of the array.
The running sum at position i is defined as: runningSum[i] = sum(nums[0] + nums[1] + ... + nums[i])
Goal: Transform the input array so that each element becomes the sum of all elements from the start up to that position.
Example: If nums = [1, 2, 3, 4], then:
• Position 0: sum of [1] = 1
• Position 1: sum of [1, 2] = 3
• Position 2: sum of [1, 2, 3] = 6
• Position 3: sum of [1, 2, 3, 4] = 10
So the result is [1, 3, 6, 10]
Input & Output
Visualization
Time & Space Complexity
For each of the n positions, we sum up to i elements, giving us 1+2+3+...+n = n(n+1)/2 operations
Only using constant extra space (excluding the output array which is required)
Constraints
- 1 ≤ nums.length ≤ 1000
- -106 ≤ nums[i] ≤ 106
- Follow-up: Can you solve this in-place with O(1) extra space?