Table of Contents
IntroductionBinary Searching on Monotonic FunctionsFinding The Maximum x Such That f(x) = trueImplementation 1Implementation 2Finding The Minimum x Such That f(x) = trueExample - Maximum MedianCommon MistakesMistake 1 - Off By OneMistake 2 - Not Accounting for Negative BoundsMistake 3 - Integer OverflowLibrary Functions For Binary SearchExample - Counting HaybalesWith Built-in FunctionWithout Using Built-in FunctionsProblemsUSACOGeneralQuizBinary Search
Authors: Darren Yao, Abutalib Namazov, Andrew Wang, Qi Wang, Dustin Miao
Binary searching on arbitrary monotonic functions and built-in functions for binary search.
Table of Contents
IntroductionBinary Searching on Monotonic FunctionsFinding The Maximum x Such That f(x) = trueImplementation 1Implementation 2Finding The Minimum x Such That f(x) = trueExample - Maximum MedianCommon MistakesMistake 1 - Off By OneMistake 2 - Not Accounting for Negative BoundsMistake 3 - Integer OverflowLibrary Functions For Binary SearchExample - Counting HaybalesWith Built-in FunctionWithout Using Built-in FunctionsProblemsUSACOGeneralQuizIntroduction
Resources | ||||
---|---|---|---|---|
CSA | animation, code, lower_bound + upper_bound | |||
CPH | code, lower_bound + upper_bound, some applications | |||
CF EDU | videos, problems similar to those covered in this module | |||
KA | plenty of diagrams, javascript implementation | |||
IUSACO | module is based off this | |||
TC | similar material | |||
LC | many problems & applications |
When we binary search on an answer, we start with a search space of size which we know the answer lies in. Then each iteration of the binary search cuts the search space in half, so the algorithm tests values. This is efficient and much better than testing each possible value in the search space.
Binary Searching on Monotonic Functions
Let's say we have a boolean function f(x)
. Usually, in such problems, we want
to find the maximum or minimum value of such that f(x)
is true. Similarly
to how binary search on an array only works on a sorted array, binary search on
the answer only works if the answer function is
monotonic, meaning that it
is always non-decreasing or always non-increasing.
x
Such That f(x) = true
Finding The Maximum We want to construct a function lastTrue
such that lastTrue(lo, hi, f)
returns the last x
in the range [lo,hi]
such that f(x) = true
. If no such
x
exists, then lastTrue
should return lo-1
.
This can be done with binary search if f(x)
satisfies both of the following
conditions:
- If
f(x) = true
, thenf(y) = true
for all . - If
f(x) = false
, thenf(y) = false
for all .
For example, if f(x)
is given by the following function:
f(1) = true f(2) = true f(3) = true f(4) = true f(5) = true f(6) = false f(7) = false f(8) = false
then lastTrue(1, 8, f) = 5
and lastTrue(7, 8, f) = 6
.
Implementation 1
Verify that this implementation doesn't call f
on any values outside of the
range [lo, hi]
.
#include <bits/stdc++.h>using namespace std;int last_true(int lo, int hi, function<bool(int)> f) {// if none of the values in the range work, return lo - 1lo--;while (lo < hi) {// find the middle of the current range (rounding up)int mid = lo + (hi - lo + 1) / 2;if (f(mid)) {
See Lambda Expressions if you're not familiar with the syntax used in the main function.
Implementation 2
This approach is based on interval jumping. Essentially, we start from the beginning of the array, make jumps, and reduce the jump length as we get closer to the target element. We use powers of 2, very similiar to Binary Jumping.
#include <bits/stdc++.h>using namespace std;int last_true(int lo, int hi, function<bool(int)> f) {lo--;for (int dif = hi - lo; dif > 0; dif /= 2) {while (lo + dif <= hi && f(lo + dif)) { lo += dif; }}return lo;}
x
Such That f(x) = true
Finding The Minimum We want to construct a function firstTrue
such that firstTrue(lo, hi, f)
returns the first x
in the range [lo,hi]
such that f(x) = true
. If no such
x
exists, then firstTrue
should return hi+1
.
Similarly to the previous part, this can be done with binary search if f(x)
satisfies both of the following conditions:
- If
f(x)
is true, thenf(y)
is true for all . - If
f(x)
is false, thenf(y)
is false for all .
We will need to do the same thing, but when the condition is satisfied, we will cut the right part, and when it's not, the left part will be cut.
#include <bits/stdc++.h>using namespace std;int first_true(int lo, int hi, function<bool(int)> f) {hi++;while (lo < hi) {int mid = lo + (hi - lo) / 2;if (f(mid)) {hi = mid;} else {
Example - Maximum Median
Focus Problem – try your best to solve this problem before continuing!
Statement: Given an array of integers, where is odd, we can perform the following operation on it times: take any element of the array and increase it by . We want to make the median of the array as large as possible after operations.
Constraints: and is odd.
To get the current median, we first sort the array in ascending order.
Now, notice that to increase the current value to a value, say . All values currently greater than or equal to the median, must remain greater than or equal to the median.
For example, say we have the sorted array . The current median is , so to increase the median to , we have to increase the current median by and we also have to increase the 's to .
Following this idea, to increase the median to , we need to increase all values in the second half of the array to some value greater than or equal to .
Then, we binary search for the maximum possible median. We know that the number of operations required to raise the median to increases monotonically as increases, so we can use binary search. For a given median value , the number of operations required to raise the median to is
If this value is less than or equal to , then can be the median, so our
check function returns true
. Otherwise, cannot be the median, so our check
function returns false
.
#include <bits/stdc++.h>using namespace std;int last_true(int lo, int hi, function<bool(int)> f) {lo--;while (lo < hi) {int mid = lo + (hi - lo + 1) / 2;if (f(mid)) {lo = mid;} else {
Here we write the check function as a lambda expression.
Common Mistakes
Mistake 1 - Off By One
Consider the code from CSAcademy's Binary Search on Functions.
long long f(int x) { return (long long)x * x; }int sqrt(int x) {int lo = 0;int hi = x;while (lo < hi) {int mid = (lo + hi) / 2;if (f(mid) <= x) {lo = mid;} else {hi = mid - 1;}}return lo;}
This results in an infinite loop if left=0
and right=1
! To fix this, set
middle = (left+right+1)/2
instead.
Mistake 2 - Not Accounting for Negative Bounds
Consider a slightly modified version of firstTrue
:
#include <functional>using namespace std;int first_true(int lo, int hi, function<bool(int)> f) {hi++;while (lo < hi) {int mid = (lo + hi) / 2;if (f(mid)) {hi = mid;
This code does not necessarily work if lo
is negative! Consider the following
example:
int main() {// outputs -8 instead of -9cout << first_true(-10, -10, [](int x) { return false; }) << "\n";// causes an infinite loopcout << first_true(-10, -10, [](int x) { return true; }) << "\n";}
This is because dividing an odd negative integer by two will round it up instead of down.
#include <functional>using namespace std;int first_true(int lo, int hi, function<bool(int)> f) {hi++;while (lo < hi) {int mid = lo + (hi - lo) / 2;if (f(mid)) {hi = mid;
Mistake 3 - Integer Overflow
The first version of firstTrue
won't work if hi-lo
initially exceeds
INT_MAX
, while the second version of firstTrue
won't work if lo+hi
exceeds
INT_MAX
at any point during execution. If this is an issue, use long long
s
instead of int
s.
Library Functions For Binary Search
Resources | ||||
---|---|---|---|---|
CPP | with examples |
Example - Counting Haybales
Focus Problem – try your best to solve this problem before continuing!
View Internal SolutionAs each of the points are in the range , storing locations of haybales in a boolean array and then taking prefix sums of that would take too much time and memory.
Instead, let's place all of the locations of the haybales into a list and sort it. Now we can use binary search to count the number of cows in any range in time.
With Built-in Function
We can use the the built-in
lower_bound
and
upper_bound
functions.
#include <bits/stdc++.h>using namespace std;void setIO(string name = "") { // name is nonempty for USACO file I/Oios_base::sync_with_stdio(0);cin.tie(0); // see Fast Input & Output// alternatively, cin.tie(0)->sync_with_stdio(0);if (!name.empty()) {freopen((name + ".in").c_str(), "r", stdin); // see Input & Outputfreopen((name + ".out").c_str(), "w", stdout);
Without Using Built-in Functions
#include <bits/stdc++.h>using namespace std;void setIO(string name = "") { // name is nonempty for USACO file I/Oios_base::sync_with_stdio(0);cin.tie(0); // see Fast Input & Output// alternatively, cin.tie(0)->sync_with_stdio(0);if (!name.empty()) {freopen((name + ".in").c_str(), "r", stdin); // see Input & Outputfreopen((name + ".out").c_str(), "w", stdout);
Problems
USACO
Status | Source | Problem Name | Difficulty | Tags | |
---|---|---|---|---|---|
Silver | Easy | Show TagsBinary Search, Sorted Set | |||
Silver | Easy | Show TagsBinary Search, Sorting | |||
Silver | Easy | Show TagsBinary Search, Sorting | |||
Silver | Normal | Show TagsBinary Search, Sorting | |||
Silver | Hard | Show TagsBinary Search | |||
Gold | Hard | Show Tags2P, Binary Search, Greedy, Sorting | |||
Silver | Very Hard | Show TagsBinary Search, Sqrt | |||
Platinum | Insane | Show TagsBinary Search, Sorting |
General
Status | Source | Problem Name | Difficulty | Tags | |
---|---|---|---|---|---|
CF | Easy | Show TagsBinary Search | |||
CSES | Easy | Show TagsBinary Search | |||
CSES | Easy | Show TagsBinary Search | |||
CF | Easy | Show TagsBinary Search | |||
CC | Normal | Show Tags2D Prefix Sums, Binary Search | |||
CSES | Normal | Show TagsBinary Search | |||
CF | Normal | Show TagsBinary Search, Prefix Sums | |||
CF | Normal | Show TagsBinary Search | |||
CF | Normal | Show TagsBinary Search | |||
CF | Normal | Show TagsBinary Search, Greedy, Prefix Sums | |||
CF | Normal | Show TagsBinary Search, Prefix Sums | |||
AC | Hard | Show TagsBinary Search, Prefix Sums, Sorting | |||
CF | Hard | Show TagsBinary Search, Prefix Sums | |||
CF | Hard | Show TagsBinary Search, Priority Queue | |||
CF | Hard | Show TagsBinary Search | |||
CF | Hard | Show TagsBinary Search | |||
Baltic OI | Very Hard | Show TagsBinary Search, Stack |
Quiz
What are the worst and best case time complexities for searching for a number in a sorted array of size with the following code?
bool binary_search(int x, std::vector<int> &a) {int l = 0;int r = a.size() - 1;while (l < r) {int m = (l + r) / 2;if (a[m] == x) {return true;} else if (x > a[m]) {l = m + 1;} else {r = m - 1;}}return false;}
Module Progress:
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!