Binary Search · Pattern 2 of 8
Lower bound, upper bound, floor and ceiling
Find where a value would go: the first element ≥ x (lower bound) or > x (upper bound). Floor, ceiling, first and last position and counts all come from these two.
Watch the lesson
Concepts first, then the problem and every approach, step by step.
Space to play or pause · ← → to jump a step · click or drag the bar to seek
When to use it
- Where would x be inserted to keep the array sorted?
- First or last position of a value, or how many times it appears
- Closest, floor (largest ≤ x) or ceiling (smallest ≥ x)
The template
// first index i with a[i] >= target; a.length if there is none
int lowerBound(int[] a, int target) {
int lo = 0, hi = a.length; // the answer is in [lo, hi]
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (a[mid] < target) lo = mid + 1; // too small: answer is right of mid
else hi = mid; // mid could be the answer
}
return lo;
}
// first index i with a[i] > target: the same loop with <=
int upperBound(int[] a, int target) {
int lo = 0, hi = a.length;
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (a[mid] <= target) lo = mid + 1;
else hi = mid;
}
return lo;
} def lower_bound(a, target): # first i with a[i] >= target
lo, hi = 0, len(a) # the answer is in [lo, hi]
while lo < hi:
mid = (lo + hi) // 2
if a[mid] < target:
lo = mid + 1 # too small: answer is right of mid
else:
hi = mid # mid could be the answer
return lo
def upper_bound(a, target): # first i with a[i] > target
lo, hi = 0, len(a)
while lo < hi:
mid = (lo + hi) // 2
if a[mid] <= target:
lo = mid + 1
else:
hi = mid
return lo
# the same as bisect.bisect_left and bisect.bisect_right // first index i with a[i] >= target; a.size() if there is none
int lowerBound(const vector<int>& a, int target) {
int lo = 0, hi = a.size(); // the answer is in [lo, hi]
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (a[mid] < target) lo = mid + 1; // too small: answer is right of mid
else hi = mid; // mid could be the answer
}
return lo;
}
// first index i with a[i] > target: the same loop with <=
int upperBound(const vector<int>& a, int target) {
int lo = 0, hi = a.size();
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (a[mid] <= target) lo = mid + 1;
else hi = mid;
}
return lo;
} // first index i with a[i] >= target; a.length if there is none
function lowerBound(a, target) {
let lo = 0, hi = a.length; // the answer is in [lo, hi]
while (lo < hi) {
const mid = (lo + hi) >>> 1;
if (a[mid] < target) lo = mid + 1; // too small: answer is right of mid
else hi = mid; // mid could be the answer
}
return lo;
}
// first index i with a[i] > target: the same loop with <=
function upperBound(a, target) {
let lo = 0, hi = a.length;
while (lo < hi) {
const mid = (lo + hi) >>> 1;
if (a[mid] <= target) lo = mid + 1;
else hi = mid;
}
return lo;
} The variations
-
1Lower bound: first element ≥ x
The insert position, the ceiling and the first occurrence are all the lower bound. If
lo == nthere is no such element.Now you solve -
2Upper bound: first element > x, and floor
The upper bound skips every copy of x. One step left of it is the floor: the largest element ≤ x, which also answers "the latest event at or before time t".
Now you solve -
3Both bounds: first, last and count
First position = lower bound. Last position = upper bound − 1. Count = upper bound − lower bound. Two O(log n) searches instead of a scan.
Worked example Find First and Last Position of Element in Sorted Array Medium ▶ Watch the walkthroughNow you solve -
4Nearest value
Find the lower bound, then compare the element there with the one just before it. The closer one is the answer; with a window, slide left or right from there.
Now you solve- Heaters Medium Stuck? solution
-
5More practice
More problems that use this pattern. Try them once the variations above feel easy.
Problems
Mistakes to avoid
- Starting with
hi = n - 1: then "insert at the end" can never be returned. - Reading
a[lo]without checkinglo < nfirst. - Floor is
upperBound(x) - 1, notlowerBound(x) - 1: the two differ when x is in the array.
Lesson done
Next: Binary search on a condition: first true →Mark it done once you can write the template from memory and you have solved the worked examples yourself.