PythiloLesson 1 of 8 · progress kept in this browser

Answer range sums with a prefix table

When many questions ask about slices of the same list, adding every slice repeats work. Store a running total before each position. A range then needs two lookups and one subtraction. Keep an initial zero so ranges beginning at index zero follow exactly the same rule as all other ranges.

values = [3, -2, 5]
prefix = [0, 3, 1, 6]
print(prefix[3] - prefix[1])
Your task

Write range_sums(values, queries). Each (left, right) uses the half-open slice values[left:right], with 0 <= left <= right <= len(values). Return one sum per query without modifying either input.