Workspace Running Sum
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Running Sum visualizer

Produce a new array whose entry at i is the total from index zero through i.

EXECUTION LAB

Produce a new array whose entry at i is the total from index zero through i.

Input and constraints
  • Use at most 32 integers. Each numeric input is between -1000 and 1000.

Code inspector

def solve(values, target):
a = values.copy()
total = 0
i = 0
while (i < len(a)):
total = (total + a[i])
a[i] = total
i = (i + 1)
return a

Visualizer

Step 1 of 5
03i
1-2
26
34

Extend the prefix ending at the current value

Extend the prefix ending at 0.

Variable watch

i 0total 0

Step explanation

Extend the prefix ending at 0.

comparisons 1reads 0writes 0swaps 0recursiveCalls 0
Educational simulation · O(n) time · O(n) output, O(1) auxiliary space

Worked explanation

The display at a checkout shows the total after each item.

Guided solution

Keep the prefix sum in an accumulator as the input advances.

O(n) time · O(n) output, O(1) auxiliary auxiliary space

Examples

Worked example · {"values":[3,-2,6,4],"target":0} ⇒ [3,1,7,11]

Edge case 1 · {"values":[],"target":0} ⇒ []

Edge case 2 · {"values":[5],"target":0} ⇒ [5]

Progressive hints

Hint 1The display at a checkout shows the total after each item.
Hint 2Keep the prefix sum in an accumulator as the input advances.
Hint 3Step through the worked example and inspect the highlighted variables before each update.

Common mistake

Resetting the accumulator at each item loses previous contributions.

Interview follow-up

How do two prefix totals answer a range-sum query?

Continue learning

Practice related DSA questions in the same topic.