Implementation of C lower_bound
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
lower_bound is a binary search algorithm that finds the first position in a sorted array where a value could be inserted without breaking the sort order. In C++ STL, it returns an iterator to the first element that is not less than the target value. C does not have this function built in, but it can be implemented with a simple binary search loop. The algorithm runs in O(log n) time and is fundamental for range queries, sorted insertions, and counting elements in sorted arrays.
C Implementation
The key insight is that lo always converges to the first index where arr[index] >= value.
Upper Bound Implementation
upper_bound finds the first element strictly greater than the value:
Counting Occurrences in a Sorted Array
Combine lower_bound and upper_bound to count how many times a value appears:
Generic Implementation with Comparators
C++ STL Reference
Python Equivalent
Use Cases
Common Pitfalls
- Integer overflow in midpoint calculation:
(lo + hi) / 2overflows for large values ofloandhi. Always uselo + (hi - lo) / 2to compute the midpoint safely. - Array must be sorted:
lower_boundassumes the array is sorted in ascending order. Using it on an unsorted array produces incorrect results. Sort the array first withqsortor ensure it is maintained in sorted order. - Off-by-one with the return value:
lower_boundreturnsn(one past the last element) when the value is greater than all elements. Always checkidx < nbefore accessingarr[idx]to avoid out-of-bounds access. - Confusing lower_bound and upper_bound:
lower_boundreturns the first position wherearr[pos] >= value.upper_boundreturns the first position wherearr[pos] > value. For counting occurrences ofvalue, you need both. - Using
<=instead of<in the comparison: The loop condition useslo < hi, notlo <= hi. Using<=causes an infinite loop whenlo == hibecause the search space never shrinks to empty.
Summary
lower_boundfinds the first index wherearr[index] >= valueusing binary search in O(log n)- The implementation uses
loandhipointers withlo < hias the loop condition upper_boundis identical but uses<=in the comparison instead of<- Combine both to count occurrences:
upper_bound(v) - lower_bound(v) - Use
lo + (hi - lo) / 2for safe midpoint calculation - In C++, use
std::lower_bound; in Python, usebisect.bisect_left
Related reading
- Implementation of distributed greedy algorithm for finding maximum independent set
- Implementation of locality-sensitive hashing with min-hash
- Implementation of Logistic regression with Gradient Descent in Java
- Implementation of March memory testing algorithm
- Implementations of count_until and accumulate_until?
- Implementing condition_variable timed_wait correctly
- Implementing a balanced binary search tree?
- Implementing a depth-first tree iterator in Python

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.