Peak Index in a Mountain Array

MediumBinary Search
Next
Binary SearchArray

A mountain array strictly increases up to a single peak and then strictly decreases. Find the index of the peak in O(log n).

Input

Line 1: n. Line 2: the mountain array.

Output

Print the index of the peak element.

Constraints

3 <= n <= 10^5 The array is guaranteed to be a valid mountain with distinct adjacent values.

Example 1
Input
3
0 1 0
Output
1

The peak is the middle element.

Example 2
Input
5
0 2 5 3 1
Output
2

5 is the largest value, at index 2.

Hints

Peak Index in a Mountain Array - Medium Binary Search Problem | Skill Shiksha