Find Peak Index

mediumBinary Search

You are given an array of $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ which first strictly increases to a peak element and then strictly decreases. This kind of array is called a mountain array.

Your task is to find the index of the peak element (the maximum element in the array).

Note: The array is guaranteed to be a mountain array. That is, there exists an index $$$0 < i < n$$$ such that: $$$a_0 < a_1 < \dots < a_{i-1} < a_i > a_{i+1} > \dots > a_{n-1}$$$

Input Format:

The first line contains an integer $$$n$$$ $$$(3 \le n \le 10^5)$$$ — the size of the array.

The second line contains $$$n$$$ space-separated integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^9)$$$ forming a mountain array.

Output Format:

Print a single integer: the 0-based index of the peak element in the array.

Examples:

Example 1:

Input:

3
1 3 2

Output:

1

Example 2:

Input:

5
2 4 8 6 1

Output:

2

Code

Loading Editor...