Peak Index in a Mountain Array - Problem
You are given an integer mountain array arr of length n where the values increase to a peak element and then decrease.
Return the index of the peak element.
Your task is to solve it in O(log(n)) time complexity.
What is a mountain array?
A mountain array is an array where:
- Array length is at least 3
- There exists some index
iwherearr[0] < arr[1] < ... < arr[i-1] < arr[i] > arr[i+1] > ... > arr[n-1]
Input & Output
Example 1 — Basic Mountain
$
Input:
arr = [0,1,0]
›
Output:
1
💡 Note:
The peak element is at index 1 with value 1. Array goes 0→1→0 forming a mountain.
Example 2 — Larger Mountain
$
Input:
arr = [0,2,1,0]
›
Output:
1
💡 Note:
The peak element is at index 1 with value 2. Array goes 0→2→1→0.
Example 3 — Multi-Element Mountain
$
Input:
arr = [0,10,5,2]
›
Output:
1
💡 Note:
The peak element is at index 1 with value 10. After the peak, values decrease: 10→5→2.
Constraints
- 3 ≤ arr.length ≤ 105
- 0 ≤ arr[i] ≤ 106
- arr is guaranteed to be a mountain array
Visualization
Tap to expand
Understanding the Visualization
1
Input
Mountain array [0,1,0] - increases then decreases
2
Process
Find the index where the peak occurs
3
Output
Return index 1 (the peak position)
Key Takeaway
🎯 Key Insight: Use the slope direction to guide binary search towards the peak
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code