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

  1. Lesson 1

    Estimate work from n, query count and loop nesting.

  2. Lesson 2

    Build a one-dimensional prefix sum with an initial zero.

  3. Lesson 3

    Derive the inclusive range formula and check endpoints.

  4. Lesson 4

    Replace sums with cumulative counts for a category.

  5. 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.