Find the first position where a condition becomes true, with no infinite loops.
Every position before lo is false and every position from hi on is true, counting n as true, so the first true position lies between lo and hi.
Use it when a condition is false for some positions and then true for ever, and you want the first position where it is true. Signal words: the first one that, from some point on, once it breaks it stays broken, the smallest position where.
Start: lo = 0 and hi = n, one past the last position. The window [lo, hi) holds the positions still in question.
Step: while lo < hi, check mid = lo + (hi - lo) / 2. If the condition is false at mid, set lo = mid + 1. Otherwise set hi = mid.
Stop: when lo == hi. The answer is lo, and lo == n means that no position is true.
It does not work when the condition can switch back from true to false. One flip is needed.
Cost: at most floor(log2 n) + 1 checks, O(log n), and O(1) extra space. The scan costs O(n) checks.
lo, hi and mid, so it needs O(1) extra space.def first_failing(results):
# [1] The window [lo, hi): builds before lo pass, builds from hi on fail, and build n counts as failing.
lo = 0
hi = len(results)
# [2] While the window holds a build, check the build in its middle.
while lo < hi:
mid = lo + (hi - lo) // 2
if results[mid] == 0:
# [3] A passing build rules out itself and every build before it, so lo moves past it.
lo = mid + 1
else:
# [4] A failing build may be the first, so keep it: hi moves to it.
hi = mid
# [5] The window is empty: lo is the first failing build, or -1 when no build failed.
return lo if lo < len(results) else -1public int firstFailing(int[] results) {
// [1] The window [lo, hi): builds before lo pass, builds from hi on fail, and build n counts as failing.
int lo = 0;
int hi = results.length;
// [2] While the window holds a build, check the build in its middle.
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (results[mid] == 0) {
// [3] A passing build rules out itself and every build before it, so lo moves past it.
lo = mid + 1;
} else {
// [4] A failing build may be the first, so keep it: hi moves to it.
hi = mid;
}
}
// [5] The window is empty: lo is the first failing build, or -1 when no build failed.
return lo < results.length ? lo : -1;
}int firstFailing(vector<int>& results) {
// [1] The window [lo, hi): builds before lo pass, builds from hi on fail, and build n counts as failing.
int lo = 0;
int hi = (int)results.size();
// [2] While the window holds a build, check the build in its middle.
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (results[mid] == 0) {
// [3] A passing build rules out itself and every build before it, so lo moves past it.
lo = mid + 1;
} else {
// [4] A failing build may be the first, so keep it: hi moves to it.
hi = mid;
}
}
// [5] The window is empty: lo is the first failing build, or -1 when no build failed.
return lo < (int)results.size() ? lo : -1;
}function firstFailing(results) {
// [1] The window [lo, hi): builds before lo pass, builds from hi on fail, and build n counts as failing.
let lo = 0;
let hi = results.length;
// [2] While the window holds a build, check the build in its middle.
while (lo < hi) {
const mid = lo + Math.floor((hi - lo) / 2);
if (results[mid] === 0) {
// [3] A passing build rules out itself and every build before it, so lo moves past it.
lo = mid + 1;
} else {
// [4] A failing build may be the first, so keep it: hi moves to it.
hi = mid;
}
}
// [5] The window is empty: lo is the first failing build, or -1 when no build failed.
return lo < results.length ? lo : -1;
}def first_at_least(values, target):
# [1] The window [lo, hi): values before lo are below the target, values from hi on are not, and position n counts as not below.
lo = 0
hi = len(values)
# [2] While the window holds a position, look at the value in its middle.
while lo < hi:
mid = lo + (hi - lo) // 2
if values[mid] < target:
# [3] A value below the target rules out itself and every value before it, so lo moves past it.
lo = mid + 1
else:
# [4] A value that is not below may be the first, so keep it: hi moves to it.
hi = mid
# [5] The window is empty: lo is the first position whose value is at least the target, or n.
return lopublic int firstAtLeast(int[] values, int target) {
// [1] The window [lo, hi): values before lo are below the target, values from hi on are not, and position n counts as not below.
int lo = 0;
int hi = values.length;
// [2] While the window holds a position, look at the value in its middle.
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (values[mid] < target) {
// [3] A value below the target rules out itself and every value before it, so lo moves past it.
lo = mid + 1;
} else {
// [4] A value that is not below may be the first, so keep it: hi moves to it.
hi = mid;
}
}
// [5] The window is empty: lo is the first position whose value is at least the target, or n.
return lo;
}int firstAtLeast(vector<int>& values, int target) {
// [1] The window [lo, hi): values before lo are below the target, values from hi on are not, and position n counts as not below.
int lo = 0;
int hi = (int)values.size();
// [2] While the window holds a position, look at the value in its middle.
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (values[mid] < target) {
// [3] A value below the target rules out itself and every value before it, so lo moves past it.
lo = mid + 1;
} else {
// [4] A value that is not below may be the first, so keep it: hi moves to it.
hi = mid;
}
}
// [5] The window is empty: lo is the first position whose value is at least the target, or n.
return lo;
}function firstAtLeast(values, target) {
// [1] The window [lo, hi): values before lo are below the target, values from hi on are not, and position n counts as not below.
let lo = 0;
let hi = values.length;
// [2] While the window holds a position, look at the value in its middle.
while (lo < hi) {
const mid = lo + Math.floor((hi - lo) / 2);
if (values[mid] < target) {
// [3] A value below the target rules out itself and every value before it, so lo moves past it.
lo = mid + 1;
} else {
// [4] A value that is not below may be the first, so keep it: hi moves to it.
hi = mid;
}
}
// [5] The window is empty: lo is the first position whose value is at least the target, or n.
return lo;
}This version writes lo = mid when the build passes, as if mid were still in question. A passing build is ruled out, so lo must move past it.
This version writes while lo <= hi, the test for a closed window that includes hi. Here the window is [lo, hi), so it is empty when lo == hi. With <=, an empty window is searched once more.
This version starts with hi = n - 1, as many closed-window versions do. Build n - 1 is then never in the window, and the search can never answer "none".
This version writes mid = (lo + hi) / 2 in Java or C++ and searches the whole numbers from 0 to 2,000,000,000 for the first one that is at least 1,800,000,000.
Your team lead asks for the first build that failed, but the results come from a flaky test. The results read [0, 1, 0, 0, 1, 1]: build 1 failed, then passed again. The condition is true, then false, then true.
systemdesignmastery.dev/dsa/cheatsheets/first-true-template