C++20 sections · 1024 units
Open in Course

Practice: Binary Search Variants

Search challenges

I'll give you problems requiring bounds. Find the number of elements in range [L,R][L, R] in a sorted vector using the difference between upper and lower bounds. You'll use: upper_bound(v.begin(), v.end(), R) - lower_bound(v.begin(), v.end(), L) to count elements where LxRL \leq x \leq R.

This runs in O(logn)O(\log n) time. Try variants: count elements less than xx, greater than yy, or exactly equal to zz. Each uses different combinations of lower_bound and upper_bound.