Peak Index in a Mountain Array (LeetCode 852): Solution and Explanation

Medium
Array
Binary Search

The array rises strictly to one highest value and then falls strictly. Return the index of that highest value, and do it in logarithmic time.

Intuition

Scanning left to right and stopping at the first drop finds the peak, but that is O(n). The problem asks for O(log n), which points at binary search. The question is what to compare at the midpoint, since there is no target value to look for.

Look at one step of the slope. If arr[mid] < arr[mid + 1], the mountain is still rising at mid. Every index from the start up to mid is on the climb, so none of them is the peak, and the peak must be to the right of mid.

If instead arr[mid] > arr[mid + 1], the mountain is already falling at mid. Everything after mid is lower still, so the peak is mid itself or somewhere to its left. Mid stays in the range because it might be the summit.

Each comparison throws away about half of the remaining indexes while always keeping the peak inside [lo, hi]. When the range shrinks to one index, that index is the peak.

Approach: Binary search on the slope

  1. Set lo = 0 and hi = n - 1. The peak is somewhere in this range.
  2. While lo < hi, compute mid = (lo + hi) // 2.
  3. If arr[mid] < arr[mid + 1], mid is on the rising side, so set lo = mid + 1.
  4. Otherwise mid is on the falling side or is the peak, so set hi = mid.
  5. When lo == hi, return lo.

Step-by-step example

arr = [0,1,0]
  1. Start with lo = 0, hi = 2.
  2. mid = 1. arr[1] = 1 is larger than arr[2] = 0, so the slope is falling at mid. Set hi = 1.
  3. lo = 0, hi = 1, so mid = 0. arr[0] = 0 is smaller than arr[1] = 1, so the slope is rising. Set lo = 1.
  4. Now lo == hi == 1. The loop stops and the answer is 1.

Code: Python, Java, C++

1class Solution:
2    def peakIndexInMountainArray(self, arr: List[int]) -> int:
3        lo, hi = 0, len(arr) - 1
4        while lo < hi:
5            mid = (lo + hi) // 2
6            if arr[mid] < arr[mid + 1]:
7                lo = mid + 1
8            else:
9                hi = mid
10        return lo
Press play on the visualizer and the running line lights up here.

Time and space complexity

Work as the input growsinput size n →steps
Binary search on the slope O(log n)
Golden-section search O(log n)
Linear scan O(n)
Time

O(log n)

Each iteration halves the size of the range [lo, hi], so the loop runs about log2(n) times.

Space

O(1)

Only the three indexes lo, hi and mid are stored.

Other approaches

Linear scan (time O(n), space O(1))

Walk from index 1 and return the first i where arr[i] > arr[i + 1]. It is simple and correct but does not meet the O(log n) requirement.

Golden-section search (time O(log n), space O(1))

Probe two interior points and drop the part beyond the lower one. It also runs in logarithmic time and can use fewer comparisons, but it is harder to write correctly than the slope test.

Common mistakes

  • Setting hi = mid - 1 on the falling side. Mid itself might be the peak, so it must stay in the range.
  • Using while lo <= hi with hi = mid. When lo == hi the range never shrinks and the loop runs forever.
  • Comparing with arr[mid - 1] while letting mid reach index 0, which reads out of bounds. With mid = (lo + hi) // 2 and lo < hi, mid + 1 is always a valid index.
  • Searching for the maximum value with a sort or a heap, which loses the index and costs more than a single pass.

Frequently asked questions

Why is mid + 1 always in bounds?

The loop only runs while lo < hi, and mid = (lo + hi) // 2 rounds down, so mid < hi. That makes mid + 1 <= hi, which is a valid index.

How is this different from Find Peak Element?

Find Peak Element allows many local peaks and accepts any of them. Here the array has exactly one peak, so the same slope test finds the only answer.

Can two neighbours be equal?

No. A mountain array rises strictly and falls strictly, so arr[mid] and arr[mid + 1] are always different and the comparison always points one way.