Peak Index in a Mountain Array
Medium
Topics
An array nums is a mountain: it strictly increases up to a unique peak and then strictly decreases. Return the index of the peak element. Solve in O(log n) time.
Example 1
Input: nums = [0,1,0] Output: 1
Example 2
Input: nums = [0,2,1,0] Output: 1
Example 3
Input: nums = [0,10,5,2] Output: 1
Constraints
- 3 <= nums.length <= 10^5
- 0 <= nums[i] <= 10^6
- nums forms a mountain (unique peak).
Editorial — intuition, approach, complexity and pitfalls
Intuition
The array rises then falls, so the comparison nums[mid] < nums[mid+1] says which side of the peak you are on: still rising means the peak is strictly to the right, falling means mid is the peak or the peak is to its left. That is a monotone predicate over the index, which is all binary search needs — the array itself is not sorted.
Keep hi as a candidate rather than excluding it. Using hi = mid on the falling side and lo = mid + 1 on the rising side converges on the single index that is never ruled out.
Approach
- Set
lo = 0andhi = len(nums) - 1. - While
lo < hi, takemidand compare it with its right neighbour. - Rising: the peak is above
mid, solo = mid + 1. - Falling:
midcould be the peak, sohi = mid. - Return
lo.
Dry Run
nums = [0,2,5,7,4,3,1]
lo hi mid nums[mid] nums[mid+1] verdict
0 6 3 7 4 falling -- peak is here or left
0 3 1 2 5 rising -- peak is right
2 3 2 5 7 rising -- peak is right
lo == hi == 3 answer 3Complexity
- Time — O(log n): the range halves every iteration.
- Space — O(1).
Common Pitfalls
- Reaching for a linear scan. It passes, but the problem names O(log n) explicitly, so the linear answer is a wrong answer here.
- Comparing
nums[mid]againstnums[mid-1]andnums[mid+1]both and returning early. It works, but it needs two bounds checks and the single-neighbour comparison makes the invariant easier to state. - Initialising
hi = len(nums). On a guaranteed mountain this is latent rather than fatal — every strict mountain up to length 9 still returns the right index, because the peak can never be the last element solonever reachesn-1andnums[mid+1]stays in range. It bites immediately on the sibling problem find peak element, where no mountain is promised. Set the bound correctly rather than relying on the guarantee. - Mixing
hi = midwith awhile lo <= hiloop, which never terminates. - Returning
midafter the loop.midis stale; the converged bound islo.
Full Solution — all languages
class Solution:
def peakIndexInMountainArray(self, nums):
lo, hi = 0, len(nums) - 1
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < nums[mid + 1]:
lo = mid + 1
else:
hi = mid
return loimport java.util.*;
class Solution {
public int peakIndexInMountainArray(int[] nums) {
int lo = 0, hi = nums.length - 1;
while (lo < hi) {
int mid = (lo + hi) / 2;
if (nums[mid] < nums[mid + 1]) lo = mid + 1;
else hi = mid;
}
return lo;
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: int peakIndexInMountainArray(vector<int>& nums) {
int lo = 0, hi = (int)nums.size() - 1;
while (lo < hi) {
int mid = (lo + hi) / 2;
if (nums[mid] < nums[mid + 1]) lo = mid + 1;
else hi = mid;
}
return lo;
}
};/**
* @param {number[]} nums
* @return {number}
*/ var peakIndexInMountainArray = function(nums) {
let lo = 0, hi = nums.length - 1;
while (lo < hi) {
const mid = (lo + hi) >> 1;
if (nums[mid] < nums[mid + 1]) lo = mid + 1;
else hi = mid;
}
return lo;
};