In the mountainous terrain surrounding Prime City, the Royal Surveyor must find a peak ÔÇö a point higher than its neighbors. A peak element in an array is an element that is greater than or equal to its neighbors. For the first and last elements, only one neighbor needs to be checked. "Binary search isn't just for sorted arrays," the Surveyor says. "If the middle element is smaller than its right neighbor, there must be a peak to the right. If it's smaller than its left neighbor, there must be a peak to the left. This is the key insight." Given an array of N integers, find the index of any peak element. The array may have multiple peaks; return any one. Output the index (0-based). Constraints: 1 <= N <= 10^5, -10^9 <= array[i] <= 10^9, adjacent elements are not equal Input: 7 1 2 3 1 5 6 4 Output: 2 Input: 3 1 2 3 Output: 2
Constraints:
1 <= N <= 10^5, -10^9 <= array[i] <= 10^9, adjacent elements not equal
Tags:
