Find Any Local Maximum in an Array

Implement findPeakElement

A peak element is one that's strictly greater than both of its neighbors (array edges count as negative infinity on the missing side). Given an array nums where adjacent elements are never equal, return the index of any peak. Aim for O(log n) time. Comparing nums[mid] with nums[mid + 1] tells you which direction climbs toward higher ground — follow it, and you're guaranteed to reach a peak.

Example 1:

Input: nums = [1,2,3,1]

Output: 2

Example 2:

Input: nums = [1,2,1]

Output: 1

Example 3:

Input: nums = [1]

Output: 0

+ 4 hidden test cases run on Submit.

Constraints:

  • 1 ≤ nums.length ≤ 1000
  • -2³¹ ≤ nums[i] ≤ 2³¹ - 1
  • nums[i] ≠ nums[i + 1] for every valid i
  • If multiple peaks exist, returning the index of any one of them is accepted

nums =

[1, 2, 3, 1]