Peak Index in a Mountain Array

Medium

Description

Given a mountain array (strictly increases then strictly decreases), return the index of the peak element. Solve in O(log n) time using binary search.

Examples

Input:arr = [0,2,1,0]
Output:1
Explanation:

Peak is at index 1 with value 2.

Input:arr = [1,3,8,12,4,2]
Output:3
Explanation:

The array increases from 1→3→8→12, then decreases from 12→4→2. The peak element 12 is at index 3.

Input:arr = [3,9,8,6,4]
Output:1
Explanation:

The array increases from 3→9, then decreases from 9→8→6→4. The peak element 9 is at index 1, demonstrating a case where the peak is near the beginning.

Constraints

  • 3 ≤ arr.length ≤ 10⁵

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!