An element of the integer array nums is a peak when it is strictly greater than both of its neighbours. Treat the positions just outside the array as minus infinity, so the first element only has to beat the second, the last only has to beat the one before it, and the only element of a one-element array is a peak.
Return the index of any peak. Neighbouring values are never equal, so at least one peak always exists. When there are several, every one of them is accepted.
Aim for O(log n) time. The judge only checks that the index you return is a peak, so it can't tell how you found it.
9 beats both 5 and 4. It is the only peak.
7 at index 5 is higher than 4 and 2, so 5 is accepted. So is 1: the 8 there is higher than 6 and 3.
The values only fall. 7 beats 4, and its left side is outside the array, so index 0 is the peak.
Constraints
1 <= nums.length <= 1000-2³¹ <= nums[i] <= 2³¹ - 1nums[i] != nums[i + 1] for every valid i.Compare the middle element with its right neighbour. If the right one is higher, walking uphill to the right must reach a peak before the array ends, since the outside counts as minus infinity. If it's lower, a peak lies at the middle or to its left.
2