Running Sum of 1d Array - Problem
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.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [1,2,3,4]
›
Output:
[1,3,6,10]
💡 Note:
Running sums: [1, 1+2, 1+2+3, 1+2+3+4] = [1,3,6,10]
Example 2 — Single Element
$
Input:
nums = [1]
›
Output:
[1]
💡 Note:
Only one element, so running sum is just [1]
Example 3 — With Negative Numbers
$
Input:
nums = [1,1,1,1,1]
›
Output:
[1,2,3,4,5]
💡 Note:
Running sums of identical elements: [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1]
Constraints
- 1 ≤ nums.length ≤ 1000
- -106 ≤ nums[i] ≤ 106
Visualization
Tap to expand
Understanding the Visualization
1
Input Array
Original numbers: [1,2,3,4]
2
Calculate Running Sums
Each position accumulates sum up to that point
3
Output Array
Running sums: [1,3,6,10]
Key Takeaway
🎯 Key Insight: Each running sum builds upon the previous one, allowing O(n) solution
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code