Find Peak Element

A peak element is an element that is strictly greater than its neighbors.

Given a 0-indexed integer array nums, find a peak element and return its index. If the array contains multiple peak elements, you may return the index of any one of them.

You may imagine that nums[-1] = -∞ and nums[n] = -∞, where n is the length of nums. In other words, elements outside the array are considered smaller than any array element.

Your solution must run in O(log n) time.

Example 1
Inputnums = [1,2,3,1]
Output2
The element 3 at index 2 is greater than both of its neighbors, so it is a peak.
Example 2
Inputnums = [1,2,1,3,5,6,4]
Output5
The element 6 at index 5 is greater than both of its neighbors, so it is a valid peak.

Constraints

  • 1 <= nums.length <= 1000
  • -2^31 <= nums[i] <= 2^31 - 1
  • nums[i] != nums[i + 1] for all valid i

Asked at 33 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate