Peak Index in Mountain Array

Med
Concept
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Peak Index in Mountain Array

Approach

Because the array increases then decreases, compare mid with mid+1. If rising, peak is to the right; if falling, peak is at mid or to the left.

Complexity Analysis

Time
O(log n)
Space
O(1)

Pattern

Binary Search (Boundary Finding)

Why It Works

The mountain shape guarantees a directional slope. Each step moves toward the slope change, so left/right bounds narrow until only the peak index remains.

Updated Feb 2026