Five minutes. One pattern. Stay fluent.

Daily etudes

Till ranges

Share task

Today’s task: “Till ranges”. Practice the Prefix sum pattern! Given an integer array days and an array of queries queries, return one sum per query in the original query order. Each query [l, r] asks fo... https://etuder.dev

Given an integer array days and an array of queries queries, return one sum per query in the original query order. Each query [l, r] asks for the sum from index l through index r, inclusive. Indices start at zero and satisfy 0 ≤ l ≤ r < len(days). Values may be negative. If there are no queries, return an empty array.

Example:

till_ranges([2, 8, 3, 5], [[0, 1], [1, 3]])  →  [10, 16]

Indices 0–1: 2+8=10. Indices 1–3: 8+3+5=16.

till_ranges([5], [[0, 0]])  →  [5]

A query covering one element is just that number.

Pattern: prefix_sum - build prefix once; each query is prefix[r+1] - prefix[l]