Lessons 11–15 · About 60 min per lesson

Binary search on the answer

Write a feasibility check and find the smallest or largest valid value in a monotone range.

Lesson breakdown

  1. Lesson 11

    Derive a feasibility predicate from the problem statement.

  2. Lesson 12

    Prove that feasibility changes monotonically with the candidate value.

  3. Lesson 13

    Find the first feasible value with inclusive bounds.

  4. Lesson 14

    Derive a search range that contains the answer.

  5. Lesson 15

    Use enumeration to check minimum, maximum and exact-feasibility cases.

Worked example

Two workers produce one item every 2 and 3 minutes respectively. When are 5 items first ready?

How to solve it

The count floor(t/2)+floor(t/3) is nondecreasing. It is 3 at time 5 and 5 at time 6, so the answer is 6.

Assignment

Given positive production periods and a nonnegative target, return the earliest nonnegative time and compare on small inputs.

Check your result

[2,3] with target 5 → 6; [4] with target 1 → 4; target 0 → 0.