Given an array of integers, write a code to find peak element in array. The array may contain multiple peak elements, in that case, return anyone peak element.
Peak Element
Peak element is an element which greater than it’s neighbours. For strictly decreasing array, the first element is the peak element. And for strictly increasing array the last element is the peak element.
For example:
Example 1:
Input: {2, 3, 4, 7, 5} , Output: 7
The element 7 is greater than it’s neighbours (4 and 5).
Example 2:
Input: {8, 7, 6, 5, 4}, Output: 8
In this example, An array is strictly decreasing, so the first element is the peak element.
Example 3:
Input: {2, 3, 4, 5, 6}, Output: 6
An array is strictly increasing, so the last element is the peak element.
Example 4:
Input: {2, 2, 2, 2, 2}, Output: 2
All the array element is the same, So every element in this array is the peak element.
→