Peak Index in Mountain Array

medium

Find the index of the peak element in a mountain array (strictly increasing then decreasing)

Peak Index in Mountain Array

Key Insight

Compare middle with next element; if it is increasing, peak is right; else peak is at mid or left.

Step 1Setup
Increasing then decreasing
L
R
0
0
2
1
3
2
5
3
4
4
1
5
Search space: [0..5]

Mountain array [0, 2, 3, 5, 4, 1].

1 / 3

Learn the Pattern

Practice the Code

Step-by-Step Walkthrough: Peak Index in Mountain Array

Compare middle with next element; if it is increasing, peak is right; else peak is at mid or left.

  1. Setup

    Mountain array [0, 2, 3, 5, 4, 1].

  2. Mid=2, Value=3

    3 < 5 (next), move left boundary to mid + 1.

  3. Converged

    left meets right at index 3, the peak index.