Find Peak Element

medium

Find any element that is strictly greater than its neighbors. nums[-1] = nums[n] = -infinity

Find Peak Element

Key Insight

If the right neighbor is larger, a peak must exist to the right (array drops to -∞ at boundary). Always move uphill.

Step 1Initialize
Any peak worksBoundary = -∞
L
R
1
0
2
1
1
2
3
3
5
4
6
5
4
6
Search space: [0..6]

Find any peak (greater than both neighbors). nums[-1] = nums[n] = -∞.

1 / 6

Learn the Pattern

Practice the Code

Step-by-Step Walkthrough: Find Peak Element

If the right neighbor is larger, a peak must exist to the right (array drops to -∞ at boundary). Always move uphill.

  1. Initialize

    Find any peak (greater than both neighbors). nums[-1] = nums[n] = -∞.

  2. Mid=3, arr[3]=3

    arr[4]=5 > arr[3]=3. Going uphill to the right. Peak must exist right.

  3. Narrow Right

    left=4, right=6, mid=5, arr[5]=6. arr[6]=4 < arr[5]=6. Going downhill right. Peak is at mid or left.

  4. Narrow Left

    left=4, right=5, mid=4, arr[4]=5. arr[5]=6 > arr[4]=5. Uphill right.

  5. Converged

    left=5, right=5. arr[5]=6 is a peak (6 > 5 and 6 > 4).

  6. Result

    Peak found at index 5, value 6.