Peak Element

Can anyone explain how the recursive (or) iterative binary search approach given in description is working for the problem.Not able to get it.


I guess the algorithm you shared only work if the data have only one peak.

Hi mohsina123 ,
There may be more than one peak element in an array, peak element means the element which is strictly greater than its neighbors.
Can we apply binary search in this problem because it’s not sorted as well?
so, the answer is yes, we can
In this problem what we do.
low=0, high=size-1 and calculate the mid element
case 1: mid element is greater than it’s left but less than it’s right so the answer is somewhere in the right , we move low=mid+1;
case-2: mid element is greater than it’s right but less than it’s left so the answer is somewhere in the left, we move high=mid-1
case-3: Finally mid element is greater then it’s left as well as right and this time this mid element is our answer.
Hope this will help you!