477. Find Peak Element in Array

MediumBinary SearchBinary SearchArray

A peak element is one that is strictly greater than its neighbors (elements outside the array are considered negative infinity). Given an array, return the index of any peak element. Because the standard binary search always converges to one specific peak, return that index. The array is given as an array of integers.

Input: An array of integers.

Output: Integer — the index of a peak element.

Examples

Example 1
Input: [1,2,3,1]
Output: 2
Explanation: 3 at index 2 is a peak.
Example 2
Input: [1,2,1,3,5,6,4]
Output: 5
Explanation: Binary search converges to index 5 (value 6).
Example 3
Input: [1]
Output: 0
Explanation: Single element is a peak.

Constraints

Asked by

MetaBloombergMicrosoftAmazonGoogleInfosys
Solve this problem in the editor →