78. Find Peak Element

MediumArrayArray

A peak element is strictly greater than its neighbors. Given nums where nums[-1]=nums[n]=-∞, return any peak element index in O(log n) time.

Input: Integer array nums where no adjacent elements are equal.

Output: Index of any peak element.

Examples

Example 1
Input: [1,2,3,1]
Output: 2
Explanation: nums[2]=3>nums[1] and nums[3].
Example 2
Input: [1,2,1,3,5,6,4]
Output: 5
Explanation: nums[5]=6 is a peak.
Example 3
Input: [1]
Output: 0
Explanation: Single element is a peak.

Constraints

Asked by

MetaBloombergMicrosoftAmazonGoogleInfosys
Solve this problem in the editor →