LeetCode #162 Medium

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.
Constraints
  • 1 <= nums.length <= 1000
  • -2³¹ <= nums[i] <= 2³¹ - 1
  • nums[i] != nums[i + 1] for all valid i.
binary-searcharray
Open on LeetCode ↗
02

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.

How to spot this pattern

"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.

03

Approach

Try it first

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).

1

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 −∞.

2

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.

3

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.

4

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.

5

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.

04

Code in Python, C++ and Java

▶1from typing import List
▶2 
▶3 
▶4class Solution:
▶5 def findPeakElement(self, nums: List[int]) -> int:
▶6 lo, hi = 0, len(nums) - 1
▶7 while lo < hi:
▶8 mid = (lo + hi) // 2
▶9 if nums[mid] < nums[mid + 1]:
▶10 lo = mid + 1
▶11 else:
▶12 hi = mid
▶13 return lo
values as a landscape−∞−∞10lo211233546546hithe shaded range holds at least one peak
lo0
hi6
Both ends fall away to −∞, so the whole array must contain a peak: the largest value is one. The search keeps a range that is still sure to contain one, and halves it each step.
values as a landscape−∞−∞10lo211233mid546546hi3 rises to 5 → a peak lies to the right
mid3
nums[mid]3
nums[mid+1]5
lo4
The slope from index 3 to 4 goes up. Walking uphill from 5 has to stop at a peak before it runs past index 6, and index 3 cannot be a peak because its right neighbour is bigger. Move lo to 4.
values as a landscape−∞−∞1021123354lo65mid46hi6 falls to 4 → peak at the middle or left
mid5
nums[mid]6
nums[mid+1]4
hi5
The slope from index 5 to 6 goes down, so 6 already beats its right neighbour. Walking uphill to the left from here must reach a peak by index 4, or 6 is the peak itself. Move hi to 5, keeping it.
values as a landscape−∞−∞1021123354lo mid65hi465 rises to 6 → a peak lies to the right
mid4
nums[mid]5
nums[mid+1]6
lo5
The slope from index 4 to 5 goes up. Walking uphill from 6 has to stop at a peak before it runs past index 5, and index 4 cannot be a peak because its right neighbour is bigger. Move lo to 5.
values as a landscape−∞−∞102112335465lo hi46one index left → index 5 is a peak
lo = hi5
answer5
The range shrank to index 5. Its value 6 beats 5 on the left and 4 on the right, so the answer is 5.
05

Common pitfalls

Dropping mid on a falling slope

✗ Wrong
else:
    hi = mid - 1
✓ Right
else:
    hi = mid

A 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 −∞

✗ Wrong
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 mid
✓ Right
if 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.

06

Edge cases

Values only rise, e.g. [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 −∞.

07

Complexity

Time
O(log n)
Space
O(1)
Each comparison halves the range: about 10 steps for the largest input of 1000 values. Only three indices are stored.
08

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.