Find Peak Element
Problem statement
A peak in an integer array is a value that is strictly greater than each neighbor it has. The first value has only a right neighbor and the last value has only a left neighbor, so you can think of the positions just outside the array as minus infinity.
You are given nums. Adjacent values are never equal, and exactly one index is a peak. Return that index.
Exactly one peak means the values strictly rise up to the peak and strictly fall after it. Either part may be empty, so the peak can be the first or the last index.
Function
findPeakElement(nums: int[]) → intExamples
Example 1
nums = [1,5,8,6,2]return = 28 at index 2 is greater than both neighbors, 5 and 6. Every other value has a larger neighbor.
Example 2
nums = [3,7,11,20]return = 3The values only go up. 20 at index 3 is greater than its left neighbor 11, and it has no right neighbor, so it is the peak.
Example 3
nums = [6]return = 0A single value has no neighbors, so index 0 is the peak.
Constraints
1 <= nums.length <= 10^4-2^31 <= nums[i] <= 2^31 - 1nums[i] != nums[i + 1]for every validi.- Exactly one index is a peak.