Problem · Array
Find a Peak Element
Learn this problemProblem statement
Given an integer array nums, return the index of its peak element. A peak is strictly greater than each neighbor that exists.
For this exercise, assume a missing neighbor beyond either endpoint has value negative infinity, adjacent values are different, and the array has exactly one peak. Your algorithm should run in O(log n) time.
Function
findPeakElement(nums: int[]) → intExamples
Example 1
nums = [1,2,3,1]return = 2The value 3 is greater than both adjacent values.
Example 2
nums = [9,6,2]return = 0The left endpoint is greater than its only neighbor.
Example 3
nums = [4]return = 0A one-element array has a peak at index 0.
Constraints
1 <= nums.length <= 10^5.-10^9 <= nums[i] <= 10^9.nums[i] != nums[i + 1]for every validi.- Exactly one index is a peak.