An index i is a peak if nums[i] is strictly greater than both of its adjacent elements, i.e. nums[i] > nums[i-1] and nums[i] > nums[i+1]. The first and last indices are never peaks, since they do not have two adjacent elements.
Return the indices of all peaks, in ascending order. If there are no peaks, return an empty array.
Examples
Example 1
Input: nums = [5, 10, 7, 8, 6, 9, 3]
Output: [1, 3, 5]
Explanation: nums[1] = 10 is greater than 5 and 7; nums[3] = 8 is greater
than 7 and 6; nums[5] = 9 is greater than 6 and 3.
Example 2
Input: nums = [1, 2, 3, 3, 2]
Output: []
Explanation: nums[2] = 3 is not strictly greater than nums[3] = 3, and no
other interior index exceeds both neighbors, so there are no peaks.
Constraints
1≤nums.length≤105
−109≤nums[i]≤109
Loading editor...
Log in to run your code. Your draft is saved in this browser.Log inSign up