WRITE · TRACE · UNDERSTAND

Prefix Sums

Build cumulative totals, then answer ranges with two lookups

Your experiment

range_sums(values, queries) returns sums for half-open ranges [left, right). Build a prefix table once, then watch each query cancel the part before left.

Python runs in your browser. Your draft stays on this device.

What to look for

prefix[i] is the total before i. The extra zero handles ranges beginning at zero and empty ranges. Trace the table being built, then compare the two boundaries for each query.

Watch your code work

Ready to experiment

Write a solution or load the worked answer, then choose Trace & check.

Step through arrays, variables and training curves produced by your own Python.