An array arr is a mountain when its numbers first go strictly up and then go strictly down. More exactly:
arr.length >= 3
- The top is at some index
i that is not the first or the last place (so 0 < i < arr.length - 1). Before it the numbers strictly increase, and after it they strictly decrease.
The array arr you receive is a mountain. Find the index i of its top and return it.
Your solution must run in O(log(arr.length)) time.
Examples
Example 1:
Input: arr = [1,4,9,6,2]
Output: 2
Explanation: The top is 9, at index 2.
Example 2:
Input: arr = [0,5,3]
Output: 1
Explanation: The top is 5, at index 1.
Example 3:
Input: arr = [2,3,4,5,1]
Output: 3
Explanation: The top is 5, at index 3.