Search code examples
algorithmarray-algorithms

How to find the number of occurences of a number in a sorted Array in O(logn)


I have a sorted Array of Integers. Given a number, how to find the number of occurences of that number in O(logn) even the worst case.


Solution

  • Binary search for the number, then binary search for the start and the end of the run.