When the search range is small, the binary answer problems are solvable by linear scaning the answer range. For example, assume the answer range is [0, 100]
, we can check 0
, then 1
, ..., then 100
, and we return the maximum number that is valid.
However, when the answer range is large, say [0, 10^5]
, linear scanning the entire answer range will get TLE.
So, instead of doing O(N)
linear scanning, we binary search in the answer range which reduces the time complexity to O(logN)
.
We can use "Binary Answer" solution if we can write a predicate function valid(i)
that has monotocity:
- If
valid(i) == true
, thenvalid(j) == true
for allj <= i
. - If
valid(i) == false
, thenvalid(j) == false
for allj >= i
.
Our goal is the find the maximum i
that valid(i) == true
.
We can use two pointers L = minVal, R = maxVal
, and keep using binary search to move the pointers towards each other until they swap order. In the end, R
will point to the largest value that is valid, L
will point to the smallest value that is invalid.
Assume the answer range is monotonically going from valid to invalid, and we are looking for the maximum valid value.
int L = minVal, R = maxVal
while (L <= R) {
int M = (L + R) / 2;
if (valid(M)) L = M + 1;
else R = M - 1;
}
return R >= minVal ? R : NOT_FOUND;
If we are looking for the minimal invalid value, simply return L <= maxVal ? L : NOT_FOUND
.
- 410. Split Array Largest Sum (Hard)
- 668. Kth Smallest Number in Multiplication Table (Hard)
- 718. Maximum Length of Repeated Subarray (Medium)
- 719. Find K-th Smallest Pair Distance (Hard)
- 778. Swim in Rising Water (Hard)
- 1044. Longest Duplicate Substring (Hard)
- 1062. Longest Repeating Substring (Medium)
- 1283. Find the Smallest Divisor Given a Threshold (Medium)
- 1300. Sum of Mutated Array Closest to Target (Medium)
- 1482. Minimum Number of Days to Make m Bouquets (Medium)
- 1648. Sell Diminishing-Valued Colored Balls (Medium)
- 1802. Maximum Value at a Given Index in a Bounded Array (Medium)
- 1870. Minimum Speed to Arrive on Time (Medium)