Peak Element Finder
Given an array of integers, find a peak element in it. An element is considered a peak if it is greater than its neighbors. For elements at the boundaries of the array, we only need to consider one neighbor.
[ 1, 3, 20, 4, 1, 0 ]
Explanation. The element 20 has neighbors 3 and 4, both of which are smaller than 20, making it a peak.
[ 5, 10, 20, 15 ]
Explanation. The element 20 has only one neighbor 15 (as it's at one boundary), and it is greater than 15.
[ 10, 20, 15, 2, 23, 90, 67 ]
Explanation. The element 20 has neighbors 10 and 15, which are smaller than 20. It is one of the peak elements.
Follow-up: Can you implement your solution to run in logarithmic time complexity?
The input array will contain at least one element. The input list may contain multiple peaks, in which case any one of them is considered a valid answer.
- Views
- 2