Peak Index in a Mountain Array

You are given an integer mountain array `arr` of length n: the values strictly increase up to some peak and then strictly decrease. Return the index of the peak element. Your solution must run in O(log n) time.

30:00

Peak Index in a Mountain Array
medium
Free written solution
Topics
Companies

You are given an integer mountain array arr of length n: the values strictly increase up to some peak and then strictly decrease. Return the index of the peak element. Your solution must run in O(log n) time.

Example 1:
Input: {"arr":[0,1,0]}
Output: 1
Constraints:
  • 3≤arr.length≤1053 \leq \text{arr.length} \leq 10^5

  • 0≤arr[i]≤1060 \leq \text{arr}[i] \leq 10^6

  • arr is guaranteed to be a mountain array.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"arr":[0,1,0]}

The peak hides somewhere in indexes 0 to 2, so the search range starts as the whole mountain.

lo

0

hi

2

mid

-

range

3 left

probes

0

001102▲lo▲hi
Variables
VariableValue
lo0
hi2
DepthFunction Call
Stack empty
0/10