Problem 95
Peak of a Mountain Array
Medium · 20 pts · Array, Binary Search
Problem
A mountain array strictly increases up to a single highest element, the peak, and then strictly decreases. Both sides have at least one element. Given a mountain array of length N >= 3, print the zero-based index of its peak. Aim for O(log N) with binary search: if A[mid] < A[mid + 1], you are on the rising slope and the peak lies to the right of mid; otherwise you are on the falling slope (or at the peak) and it lies at mid or to the left.
Input
First line: integer N. Second line: N space-separated integers forming a mountain.
Output
Print the zero-based index of the peak.
Constraints
3 <= N <= 100000; 0 <= A[i] <= 1000000
Example input
3 0 1 0
Example output
1
Explanation
In 0 1 0, the values rise from 0 to 1 and then fall back to 0. The peak value 1 is at index 1.