Builds are numbered 1..n. At some point a bug was introduced, and every build from that one onward is
broken; all earlier builds are fine. You are given n and a function is_broken(v) that returns True
if build v is broken.
Return the number of the first broken build, or -1 if no build is broken.
Examples
Input: n = 5, is_broken = lambda v: v >= 4
Output: 4
Input: n = 5, is_broken = lambda v: False
Output: -1
Input: n = 1, is_broken = lambda v: True
Output: 1
Constraints
1 <= n <= 10**9is_brokenis monotone: once it returnsTrueit returnsTruefor every larger build.- Call
is_brokenat most 40 times; checking builds one at a time is too slow.
Goals
- Binary search over a monotone boolean predicate
- Detect the 'no True value at all' case