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 Array INPUT PEAK i=0 i=1 i=2 arr = [0, 1, 0] 0 1 0 [0] [1] [2] Length n = 3 ALGORITHM STEPS 1 Initialize Pointers left=0, right=n-1 2 Binary Search Loop while left < right 3 Calculate Mid mid = (left+right)/2 4 Compare & Narrow arr[mid] vs arr[mid+1] Decision Logic: if arr[mid]<arr[mid+1] left = mid + 1 else: right = mid FINAL RESULT FOUND Peak Index: 1 OK - Verified arr[1]=1 is the peak 0 < 1 > 0 O(log n) achieved Key Insight: Binary search works because the mountain property guarantees a single peak. If arr[mid] < arr[mid+1], the peak is to the right (ascending slope). Otherwise, peak is at mid or left (descending slope). This eliminates half the search space each iteration, achieving O(log n) time complexity. TutorialsPoint - Peak Index in a Mountain Array | Binary Search Approach
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