DSA AnimatorDSA animations
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.

How to solve Running Sum of 1D Array

LeetCode 1480 · Easy · Arrays · Pattern: Prefix Sum

Why the obvious approach is slow

The brute force re-adds everything from the start for each position. That's O(n²), and it repeats the same additions again and again.

The key idea

Each running sum is the previous running sum, plus the current number. So walk left to right, and add the previous value into the current one, in place.

Remember

Prefix sum at i equals prefix sum at i −1, plus the value at i. This prefix sum idea is the base of many harder array problems.

Run the animation above on your own input, or watch the 1:13 video explanation of Running Sum of 1D Array with a dry run and Java code.

More Arrays problems