Find any peak element in an array where values are strictly greater than their neighbors.
Given an integer array nums, return the index of any peak element.
An element is a peak if it is strictly greater than its adjacent elements. For the boundaries, treat the element outside the array as negative infinity.
You may assume that:
nums[-1] = nums[n] = -∞Your goal is to return the index of any peak element in O(log n) time.
nums.nums contains at least one element.Example 1
Input
nums = [1,2,3,1]
Output
2
Explanation
nums[2] = 3 is greater than both neighbors, so index 2 is a peak.
Example 2
Input
nums = [1,2,1,3,5,6,4]
Output
1
Explanation
Index 1 is a valid peak because 2 > 1 and 2 > 1. Index 5 is also a peak, so either answer is acceptable.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.