Binary Search (First Occurance of a number)


In order to find the first occurance of a number, We only require certain modifications in our orignal binary search code. When we find the item, all we need to do is to change the value of end to mid - 1 (instead of break statement), while saving current mid. In this way we will get the first occurance of the number.

The code is.

The link to code