← Back to DSA Animator
Running Sum of 1D Array LC #1480 Easy Prefix Sum
Problem

Given an array nums, define its running sum as runningSum[i] = sum(nums[0]…nums[i]). Return the running sum of nums.

Example 1
Input: nums = [1,2,3,4]
Output: [1,3,6,10]
Explanation: Running sum: 1, 1+2=3, 1+2+3=6, 1+2+3+4=10.
Constraints: 1 ≤ nums.length ≤ 1000  |  −10⁶ ≤ nums[i] ≤ 10⁶
Try Examples
Custom:
Approach
In-Place Prefix Sum — One Pass
Start from index 1. Each nums[i] += nums[i-1] accumulates the running total. Index 0 is already its own prefix sum. O(1) space — modify in place.
Array (updated in-place)
Variables
Index i
nums[i-1]
original val
new sum
Step Logic
Press ▶ Play or Next Step to begin.
🎉
Ready
0 / 0
Select an example above and press Play.
Algorithm
1
Index 0 is already correct — skip it
2
For i = 1 to n-1: nums[i] += nums[i-1]
3
Each element now holds the sum of all previous + itself
4
Return the modified array
Time
O(n)
Space
O(1)
Why It Works

At each step, nums[i-1] already holds the sum of elements 0..i-1. Adding it to nums[i] extends the running total by one element. This builds the prefix sum left to right in a single pass with no extra array needed.