Peak Index in a Mountain Array (LeetCode 852): Solution and Explanation
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
- Set
lo = 0andhi = n - 1. The peak is somewhere in this range. - While
lo < hi, computemid = (lo + hi) // 2. - If
arr[mid] < arr[mid + 1], mid is on the rising side, so setlo = mid + 1. - Otherwise mid is on the falling side or is the peak, so set
hi = mid. - When
lo == hi, returnlo.
Step-by-step example
arr = [0,1,0]
- Start with
lo = 0,hi = 2. mid = 1.arr[1] = 1is larger thanarr[2] = 0, so the slope is falling at mid. Sethi = 1.lo = 0,hi = 1, somid = 0.arr[0] = 0is smaller thanarr[1] = 1, so the slope is rising. Setlo = 1.- 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 lo1class Solution {
2 public int peakIndexInMountainArray(int[] arr) {
3 int lo = 0, hi = arr.length - 1;
4 while (lo < hi) {
5 int mid = lo + (hi - lo) / 2;
6 if (arr[mid] < arr[mid + 1]) {
7 lo = mid + 1;
8 } else {
9 hi = mid;
10 }
11 }
12 return lo;
13 }
14}1class Solution {
2public:
3 int peakIndexInMountainArray(vector<int>& arr) {
4 int lo = 0, hi = (int)arr.size() - 1;
5 while (lo < hi) {
6 int mid = lo + (hi - lo) / 2;
7 if (arr[mid] < arr[mid + 1]) {
8 lo = mid + 1;
9 } else {
10 hi = mid;
11 }
12 }
13 return lo;
14 }
15};Time and space complexity
O(log n)
Each iteration halves the size of the range [lo, hi], so the loop runs about log2(n) times.
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 - 1on the falling side. Mid itself might be the peak, so it must stay in the range. - Using
while lo <= hiwithhi = mid. Whenlo == hithe range never shrinks and the loop runs forever. - Comparing with
arr[mid - 1]while letting mid reach index 0, which reads out of bounds. Withmid = (lo + hi) // 2andlo < hi,mid + 1is 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.