Find Peak Element
Given an array of integers nums, where nums[i] does not equal nums[i + 1] for any 'i', find a peak element and return its index. A peak element is an element that is greater than its neighbors.
For example, in the array 3, 4, 3, 2, 1, 4 is a peak element because it is greater than its neighbors 3 and 3.
Note:
- The array may contain multiple peaks, in that case, return the index to any of the peaks.
[ 1, 2, 3, 1 ]
Explanation. 3 is the peak element because it is greater than its neighbors 2 and 1.
[ 1, 2, 1, 3, 5, 6, 4 ]
Explanation. 6 is a peak element because it is greater than its neighbors 5 and 4.
[ 3, 4, 5, 6 ]
Explanation. 6 is the peak element as it is not smaller than its neighbor 5 (and there's no neighbor on the right side).
[ 6, 5, 4, 3 ]
Explanation. 6 is the peak element as it is not smaller than its neighbor 5 (and there's no neighbor on the left side).
Follow-up: Can you implement your solution with a logarithmic runtime complexity (i.e., O(log n))?
The input will be a non-empty array containing integers. The array may have one element.
- Views
- 2