Most Generalized Binary Search
Suppose we have a search space. It could be an array, a range, etc. Usually it’s sorted in ascending order. For most tasks, we can transform the requirement into the following generalized form:
- Minimize , such that
The following code is the most generalized binary search template:
Tip
What’s really nice of this template is that, for most of the binary search problems, we only need to modify three parts after copy-pasting this template, and never need to worry about corner cases and bugs in code any more:
- Correctly initialize the boundary variables
left
andright
to specify search space. Only one rule: set up the boundary to include all possible elements;- Decide return value. Is it
return left
orreturn left - 1
? Remember this: after exiting the while loop,left
is the minimal k satisfying thecondition
function;- Design the
condition
function. This is the most difficult and most beautiful part. Needs lots of practice.
Note
What makes binary search work is that there exists a function that can map elements in left half to True, and the other half to False, or vice versa. If we can find such a function, we can apply bs to find the boundary (lower_bound for example)