Skip to main content

Find the Peak Element

MediumPremium

Given an array nums , find its peak element and return its index (0-based indexing). A peak element is one that is strictly greater than its neighbors. If there are multiple peaks, you may return the index of any one of them.

It is guaranteed that at least one peak element exists in the given input array.

You should write an algorithm that runs in O(log n) time.

Examples

Input: nums = [1] Output: 0 Explanation: Single element 1 at index 0 is the peak. Input: nums = [1, 2, 3, 4, 5] Output: 4 Input: nums = [1, 2, 1, 3, 5, 6, 4] Output: 1 or 5