Find Peak Element
Find Peak Element is LeetCode 162 (Medium). A peak is a value strictly greater than both of its neighbours. Given an array nums in which no two neighbours are equal, return the index of any peak. The positions just outside the array count as −∞, so the first or the last value can be a peak.
- The solution must run in O(log n) time.
- Values cover the whole 32-bit range, from −2³¹ to 2³¹ − 1.
- 1 <= nums.length <= 1000
- -2³¹ <= nums[i] <= 2³¹ - 1
- nums[i] != nums[i + 1] for all valid i.
Intuition
The array is not sorted, but binary search does not need sorting. It needs a test that says which half surely holds an answer, and here that test is the slope at the middle.
If nums[mid] < nums[mid + 1], the values rise to the right. Keep walking uphill from there and you must stop somewhere, at the latest at the last index, whose right side is −∞. Wherever you stop is a peak. So a rising slope promises a peak to the right; a falling slope promises one at mid or to its left. One comparison discards half the range.
"Return any index that beats its neighbours" in O(log n) on unsorted data: binary search on the direction of the slope, not on a value.
Approach
Before reading on, pick a middle index and look only at it and its right neighbour. If the right one is bigger, can you prove a peak exists to the right? What if it is smaller? Then write the loop in O(log n).
Start with the whole array
Set lo = 0 and hi = n - 1. A peak exists: the largest value is bigger than both neighbours, since neighbours are distinct and the outside counts as −∞.
Rising at the middle: go right
If nums[mid] < nums[mid + 1], set lo = mid + 1. mid cannot be a peak, since its right neighbour is bigger, and the climb that starts at mid + 1 ends at a peak no further right than hi.
Falling at the middle: keep the middle
Otherwise nums[mid] > nums[mid + 1], so set hi = mid. mid stays because it may be the peak: it already beats its right neighbour.
Why the range always holds a peak
Throughout, the value left of lo is smaller than nums[lo] (or is −∞), and the value right of hi is smaller than nums[hi] (or is −∞). The largest value in the range then beats both neighbours, so it is a peak.
Return when one index is left
Loop while lo < hi; then mid < hi, so mid + 1 is always a real index. When lo == hi, that index is the peak the range was guaranteed to hold.
Code in Python, C++ and Java
lo to 4.hi to 5, keeping it.lo to 5.lo to 2.hi to 2, keeping it.hi to 2, keeping it.hi to 1, keeping it.lo to 1.Common pitfalls
Dropping mid on a falling slope
else:
hi = mid - 1else:
hi = midA falling slope means mid beats its right neighbour, so it may be the peak itself. On [1, 3, 2] the middle is the 3; skipping it leaves only index 0, and the search returns 0, whose value 1 is not a peak.
Using the smallest int as −∞
left = nums[mid - 1] if mid > 0 else -2**31
right = nums[mid + 1] if mid < n - 1 else -2**31
if nums[mid] > left and nums[mid] > right:
return midif nums[mid] < nums[mid + 1]:
lo = mid + 1
else:
hi = mid−2³¹ is a legal value, so it cannot stand in for −∞. On [-2147483648] the only value is a peak, but it is not greater than the stand-in, so the test never succeeds. Comparing mid with mid + 1 inside the range never needs a stand-in.
Edge cases
[1, 2, 3, 4]Every slope is uphill, so lo moves right until it reaches index 3. The last value is a peak because its right side counts as −∞.
Complexity
Find Peak Element FAQ
Does this find the highest peak?
No, only some peak. On [1, 3, 2, 1, 5, 4] the first middle sits on a falling slope, so the search goes left and returns index 1 (the 3), although 5 is bigger. Finding the largest value needs a full O(n) pass, since it could be anywhere.
Is a linear scan good enough?
It is correct: the first index whose value is bigger than the next one is a peak, and if there is none the last index is. But it is O(n), and this problem asks for O(log n).
Does the same idea work on a 2D grid?
Yes, with one change. In Find a Peak Element II (LeetCode 1901), take the middle column, find its largest value, and compare that with its left and right neighbours. Move toward the bigger neighbour, as here. That is O(m log n) for m rows and n columns.