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
Running Sum of 1d ArrayInput:1234Process:sum = 1sum = 1+2sum = 1+2+3sum = 1+2+3+4Output:13610Each position stores the cumulative sum up to that index
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
Asked in
Amazon 15 Microsoft 8 Google 6
85.0K Views
High Frequency
~10 min Avg. Time
4.2K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen