Find the index of the peak element in a mountain array.
You are given a mountain array arr of length n.
A mountain array has the following properties:
n >= 3peak such that:
arr[0] < arr[1] < ... < arr[peak]arr[peak] > arr[peak + 1] > ... > arr[n - 1]Your task is to return the index of the peak element.
A correct solution should be better than linear scan when possible and should work for the mountain structure described above.
arr representing a mountain array.3 <= arr.lengtharr is guaranteed to be a mountain arrayExample 1
Input
arr = [0, 2, 4, 3, 1]
Output
2
Explanation
The values increase up to 4 at index 2, then decrease afterward, so the peak index is 2.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.