Find Peak Element
Given an array of integers, find a peak element and return its index. A peak element is defined as an element that is greater than its neighbors. For elements at the boundaries of the array, we only need to check one neighbor. Assume the input array ‘nums’ will contain at least one element and may contain multiple peaks, in case of multiple peaks return the index to any one of the peak elements.
[ 1, 2, 3, 1 ]
Explanation. Element 3 has index 2 and is greater than both of its neighbors (2 and 1).
[ 1, 2, 1, 3, 5, 6, 4 ]
Explanation. Element 6 has index 5 and it's the highest peak. It is greater than its neighbors (5 and 4).
[ 1 ]
Explanation. Single element 1 is a peak by default and is the only element in the array.
Follow-up: Can you implement your solution to find all peak elements in the array?
The input array will contain at least one element. Peak comparison for the elements at the edges of the array (start and end) will only require one neighbor comparison.
- Views
- 3