easy +10 pts

Peak Element in Array

Find any index where an element is greater than or equal to its neighbors.

A peak element in an array is an element that is greater than or equal to its neighbors. For the first and last elements, only consider one neighbor. Given a list of integers `arr`, return the index of **any** peak element. It is guaranteed that at least one peak exists. You may assume the list is non-empty. If multiple peaks exist, returning any valid index is acceptable. Implement the function `find_peak(arr)` that takes a list of integers and returns an integer index.

Constraints

1 <= len(arr) <= 10^5 -10^9 <= arr[i] <= 10^9 Time: O(n) is acceptable, O(log n) is better. Space: O(1) extra.

Example

>>> find_peak([1, 3, 20, 4, 1])
2
>>> find_peak([1, 2, 3, 4])
3
>>> find_peak([5, 4, 3, 2, 1])
0
>>> find_peak([1])
0
>>> find_peak([1, 2, 2, 1])
1
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Consider the definition of peak for boundaries: if the first element is greater than or equal to the second, it's a peak.
Try scanning from left to right and stop at the first position that satisfies the peak condition.
A list that is strictly increasing always has the last element as a peak.
You don't need to modify the list; just compare neighbors.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.