Lower_bound and Upper_bound
C++
Hard
3 views
Problem Description
Find position to insert element to keep sorted order.
Real Life: Finding where to place book in sorted shelf.
Step-by-Step Logic:
1. lower_bound() finds first position >= value
2. upper_bound() finds first position > value
3. Used in sorted containers
4. Returns iterator
Official Solution
void stl_q14_bounds() {
vector<int> numbers = {10, 20, 30, 30, 40, 50};
auto lower = lower_bound(numbers.begin(), numbers.end(), 30);
auto upper = upper_bound(numbers.begin(), numbers.end(), 30);
cout << "Lower bound of 30 at position: " << (lower - numbers.begin()) << endl;
cout << "Upper bound of 30 at position: " << (upper - numbers.begin()) << endl;
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!