medium +20 pts

Find Peak Element

Locate any index where the element is strictly greater than its neighbors.

A peak element is an element that is strictly greater than its neighbors. Given an integer array `nums`, implement the function `find_peak_element(nums: list[int]) -> int` that returns the index of any peak element. You may imagine that `nums[-1] = nums[n] = -infinity` (i.e., the element at index 0 is a peak if it is greater than `nums[1]`, and the element at the last index is a peak if it is greater than `nums[n-2]`). The array may contain multiple peaks; your function can return the index of any of them.

Constraints

1 <= len(nums) <= 10^5 -10^9 <= nums[i] <= 10^9 There is guaranteed to be at least one peak element. Your solution should run in O(log n) time.

Example

find_peak_element([1,2,3,1]) -> 2
find_peak_element([1,2,1,3,5,6,4]) -> 5  # 1 is also acceptable
20 points ~25 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Think about the slope between adjacent elements: if nums[mid] < nums[mid+1], the peak is to the right; else the peak is to the left (including mid).
Use binary search to reduce the search range in half each step.
The boundary conditions are handled by assuming -infinity outside the array.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.