Find peak element
Problem Find any peak element — one strictly greater than its neighbors — and return its index (nums[-1] and nums[n] are conceptually −∞). If there are multiple peaks, return any.
Input / Output
- Input: int array nums (adjacent elements distinct).
- Output: a peak index.
Constraints
- n up to 10^5; O(log n) required — the linear scan is the baseline being beaten.
Example
- [1,2,3,1] → 2; [1,2,1,3,5,6,4] → 1 or 5.
asked …