Find the first position not less than, or greater than, a value.
Every value before lo is below the target, and every value from hi on is not, so lo ends as the lower bound.
Use it when a sorted list has repeats, and you need to know where a value starts or ends, how many there are, or where it would go. Signal words: how many are smaller, how many are equal, the first and last position of, the rank of.
Start: lo = 0 and hi = n. The window [lo, hi) holds the positions still in question.
Step: while lo < hi, set mid = lo + (hi - lo) / 2. For the lower bound, move lo to mid + 1 when values[mid] < x, and otherwise set hi = mid. For the upper bound, use values[mid] <= x for that test.
Stop: when lo == hi. The answer is lo, which can be n.
It does not work on a list that is not sorted by the same order as the test.
Cost: at most floor(log2 n) + 1 checks for each bound, O(log n), and O(1) extra space. The scan costs O(n).
lo, hi and mid, so the extra space is O(1).def lower_bound(values, x):
# [1] The window [lo, hi): values before lo are below x, 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] < x:
# [3] A value below x rules out itself and every value before it, so lo moves past it.
lo = mid + 1
else:
# [4] A value that is not below x 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 not below x, or n.
return lopublic int lowerBound(int[] values, int x) {
// [1] The window [lo, hi): values before lo are below x, 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] < x) {
// [3] A value below x rules out itself and every value before it, so lo moves past it.
lo = mid + 1;
} else {
// [4] A value that is not below x 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 not below x, or n.
return lo;
}int lowerBound(vector<int>& values, int x) {
// [1] The window [lo, hi): values before lo are below x, 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] < x) {
// [3] A value below x rules out itself and every value before it, so lo moves past it.
lo = mid + 1;
} else {
// [4] A value that is not below x 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 not below x, or n.
return lo;
}function lowerBound(values, x) {
// [1] The window [lo, hi): values before lo are below x, 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] < x) {
// [3] A value below x rules out itself and every value before it, so lo moves past it.
lo = mid + 1;
} else {
// [4] A value that is not below x 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 not below x, or n.
return lo;
}def upper_bound(values, x):
# [1] The window [lo, hi): values before lo are at most x, values from hi on are above x, and position n counts as above.
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] <= x:
# [3] A value at most x rules out itself and every value before it, so lo moves past it.
lo = mid + 1
else:
# [4] A value above x 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 above x, or n.
return lopublic int upperBound(int[] values, int x) {
// [1] The window [lo, hi): values before lo are at most x, values from hi on are above x, and position n counts as above.
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] <= x) {
// [3] A value at most x rules out itself and every value before it, so lo moves past it.
lo = mid + 1;
} else {
// [4] A value above x 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 above x, or n.
return lo;
}int upperBound(vector<int>& values, int x) {
// [1] The window [lo, hi): values before lo are at most x, values from hi on are above x, and position n counts as above.
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] <= x) {
// [3] A value at most x rules out itself and every value before it, so lo moves past it.
lo = mid + 1;
} else {
// [4] A value above x 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 above x, or n.
return lo;
}function upperBound(values, x) {
// [1] The window [lo, hi): values before lo are at most x, values from hi on are above x, and position n counts as above.
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] <= x) {
// [3] A value at most x rules out itself and every value before it, so lo moves past it.
lo = mid + 1;
} else {
// [4] A value above x 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 above x, or n.
return lo;
}This version returns mid as soon as values[mid] == x, as a search for one value does. With repeats, the middle can be any of the copies.
This version reports upper as the position of the last copy. The upper bound is the first position after the copies, so it is one too far.
This version checks whether x is present with values[lower] == x, and does not check that lower < n first.
This version reads the negative result of Arrays.binarySearch as -r, and takes it as the insertion point. The result is -(insertion point) - 1, so the insertion point is -r - 1.
Your team lead hands you a list of payments in the order they arrived: [5, 1, 9, 3, 7]. You ask for the lower bound of 7.
systemdesignmastery.dev/dsa/cheatsheets/lower-upper-bound