Skip to content

Instantly share code, notes, and snippets.

@akshatgoel
Last active March 15, 2021 00:18
Show Gist options
  • Save akshatgoel/7c7d06c428a00e8d4ba827ca4bbac88c to your computer and use it in GitHub Desktop.
Save akshatgoel/7c7d06c428a00e8d4ba827ca4bbac88c to your computer and use it in GitHub Desktop.
Binary search template
int x = -1;
int z = n / 2; // any sane large enough value to split problem space
for (int b = z; b >= 1; b /= 2) {
while (f(x+b) < f(x+b+1)) x += b;
}
int k = x+1;
for (int b = z; b >= 1; b /= 2) {
while (!ok(x+b)) x += b;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment