Practice/Meta/Leetcode 162. Find Peak Element
CodingMust
You are given an array of integers where no two adjacent elements are equal. A peak element is defined as an element that is strictly greater than its neighbors. For elements at the boundaries of the array, consider the out-of-bounds neighbor as negative infinity.
Your task is to find and return the index of any peak element in the array. If multiple peaks exist, you may return the index of any one of them.
The algorithm must run in O(log n) time complexity.
i satisfies: nums[i] > nums[i-1] and nums[i] > nums[i+1]Example 1:
Input: nums = [1, 3, 2] Output: 1 Explanation: The element at index 1 has value 3, which is greater than both neighbors (1 and 2).
Example 2:
Input: nums = [5, 3, 1] Output: 0 Explanation: The element at index 0 has value 5, greater than its right neighbor 3. The left side is treated as negative infinity.
Example 3:
Input: nums = [1, 5, 3, 6, 4] Output: 1 or 3 Explanation: Both index 1 (value 5) and index 3 (value 6) are peaks. Either answer is correct.