3.Find Peak Element
A peak element is an element that is greater than its neighbors.
Algorithm:
Check if the mid value or index mid = (l+r)/2, is the peak element or not, if yes then print the element and terminate. (check of if both the left and right neighbors are greater)
Else if the element on the left side of the middle element is greater then check for peak element on the left side, i.e. update r = mid – 1
Else if the element on the right side of the middle element is greater then check for peak element on the right side , i.e. update l = mid + 1
Corner Cases:
If input array is sorted in strictly increasing order, the last element is always a peak element. For example, 50 is peak element in {10, 20, 30, 40, 50}.
If the input array is sorted in strictly decreasing order, the first element is always a peak element. 100 is the peak element in {100, 80, 60, 50, 20}.
Solution : (Binary Search)
Time Complexity: O(log (n))
Last updated