Continue learning
Practice related DSA questions in the same topic.
Produce a new array whose entry at i is the total from index zero through i.
Produce a new array whose entry at i is the total from index zero through i.
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 aExtend the prefix ending at 0.
Extend the prefix ending at 0.
The display at a checkout shows the total after each item.
Keep the prefix sum in an accumulator as the input advances.
O(n) time · O(n) output, O(1) auxiliary auxiliary space
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]
Resetting the accumulator at each item loses previous contributions.
How do two prefix totals answer a range-sum query?