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 i where arr[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
Peak Index in a Mountain Array010Index 0Index 1Index 2Mountain shape: values increase to peak, then decreasePeak Index: 1
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
Asked in
Google 35 Facebook 28 Amazon 22
89.0K Views
High Frequency
~15 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen