Lessons 1–5 · About 60 min per lesson
Complexity and prefix sums
Rule out slow solutions using constraints and preprocess repeated interval calculations.
Lesson breakdown
- Lesson 1
Estimate work from n, query count and loop nesting.
- Lesson 2
Build a one-dimensional prefix sum with an initial zero.
- Lesson 3
Derive the inclusive range formula and check endpoints.
- Lesson 4
Replace sums with cumulative counts for a category.
- Lesson 5
Test negative values, endpoint indexes and 64-bit totals.
Worked example
For array [2,-1,4,3] with zero-based indexes, query inclusive interval [1,2].
How to solve it
P is [0,2,1,5,8], so P[3] − P[1] = 3. Preprocessing is linear and each query is constant time.
Assignment
Answer queries for the number of 1s in each interval, and use a direct count as a small-input reference.
Check your result
For [1,0,1,1], [0,3] → 3, [1,1] → 0 and [2,3] → 2.