Peak Finder in an Array
Given a non-empty array of integers, find any peak element. An element is considered a peak if it is greater than its neighbors. Note that an element at index 0 or at the last index of the array is considered a peak if it is greater than its one neighbor.
[ 1, 3, 20, 4, 1, 0 ]
Explanation. 20 is a peak element because it is greater than both of its neighbors (3 and 4).
[ 5, 10, 20, 15 ]
Explanation. 20 is a peak element because it is greater than its left neighbor (10) and has no right neighbor.
[ 10, 5, 2 ]
Explanation. 10 is a peak at the start of the array as it is only compared with its right neighbor.
[ 6, 6, 6, 6 ]
Explanation. Here, any 6 can be considered a peak since all elements are the same and adjacent comparison holds true.
Follow-up: Can you implement your solution in a way that achieves O(log n) time complexity?
The input array will contain at least one element and will contain only integers.
- Accepted
- 1/2
- Acceptance Rate
- 50.0%
- Views
- 2