95. Find Peak Element

Medium · Binary Search

A peak element is an element that is strictly greater than its neighbors. Given an array of integers, find any peak element and return its index. If the array has multiple peaks, return the index of any one of them.

You may imagine that the array is extended with -∞ on both ends, so that every element at the boundary is considered to have a neighbor with a smaller value.

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

Examples

Example 1
Input: [1, 2, 3, 1]
Output: 2
Explanation: The element at index 2 (value 3) is a peak because 3 > 2 and 3 > 1.
Example 2
Input: [1, 2, 1, 3, 5, 6, 4]
Output: 5
Explanation: The element at index 5 (value 6) is a peak because 6 > 5 and 6 > 4. Index 1 (value 2) is also a peak.

Constraints