Find Peak Element (LeetCode 162): Solution and Explanation

Medium
Array
Binary Search

A peak is an element strictly greater than its neighbors, and positions outside the array count as negative infinity. Given an array where adjacent values are never equal, return the index of any peak in logarithmic time.

Variables
No variables to display
DepthFunction Call
Stack empty
0/0

Intuition

A linear scan finds a peak easily: return the first index whose next value is smaller, or the last index if the array keeps rising. That is O(n), and the problem explicitly asks for O(log n), which is a strong hint that binary search is involved even though the array is not sorted.

The insight is that binary search does not need a sorted array, it only needs a rule that tells you which half is guaranteed to contain an answer. Look at the middle element. If its right neighbor is larger, walk mentally to the right: the values either keep rising until the end of the array, in which case the last element is a peak because the boundary is negative infinity, or they eventually drop, and the point just before the drop is a peak. Either way, a peak exists to the right of m. The same argument works on the left side when the left neighbor is larger.

If neither neighbor is larger, m itself is a peak and you can return it. Because adjacent values are never equal, there is no ambiguous flat case to handle.

So each step either finds a peak or discards half of the remaining range while keeping a guaranteed peak inside it. The array does not need to be sorted, and the problem accepts any peak, which is exactly why this greedy climb toward a larger neighbor is enough.

Approach: Binary search toward a larger neighbor

  1. Set l = 0 and r = len(nums) - 1.
  2. While l <= r, compute m = l + (r - l) // 2.
  3. If m > 0 and nums[m] < nums[m - 1], a peak exists on the left, so set r = m - 1.
  4. Else if m is not the last index and nums[m] < nums[m + 1], a peak exists on the right, so set l = m + 1.
  5. Otherwise nums[m] is larger than both neighbors (or sits at an edge), so return m.

Step-by-step example

nums = [1,2,1,3,5,6,4]
  1. l = 0 and r = 6, so m = 3 with value 3. Its left neighbor 1 is smaller, but its right neighbor 5 is larger, so l = 4.
  2. Now m = 4 + (6 - 4) // 2 = 5, with value 6. The left neighbor 5 is smaller and the right neighbor 4 is smaller.
  3. Index 5 is a peak, so the function returns 5. Index 1 (value 2) is also a valid peak, but the search happened to climb right.

Code: Python, Java, C++

1class Solution:
2    def findPeakElement(self, nums: List[int]) -> int:
3        l, r = 0, len(nums) - 1
4        
5        while l <= r:
6            m = l + ((r - l) // 2)
7            # left neighbor greater
8            if m > 0 and nums[m] < nums[m - 1]:
9                r = m - 1
10            # right neighbor greater
11            elif m < len(nums) - 1 and nums[m] < nums[m + 1]:
12                l = m + 1
13            else:
14                return m
15        return -1

Time and space complexity

Time

O(log n)

Each iteration either returns or cuts the search range roughly in half.

Space

O(1)

Only the two bounds and the midpoint are stored.

Other approaches

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

Walk from left to right and return the first index where nums[i] > nums[i + 1], or the last index if no such drop exists. It is simple and correct but O(n), so it does not meet the logarithmic requirement.

Binary search comparing only with the right neighbor (time O(log n), space O(1))

Use while l < r and compare nums[m] with nums[m + 1]: move r = m if it is a descent, else l = m + 1. When the bounds meet, they point at a peak. It needs only one comparison per step and avoids edge checks.

Common mistakes

  • Assuming the array must be sorted for binary search. Any peak is accepted, and the climbing argument only needs local comparisons.
  • Forgetting the boundary checks m > 0 and m < len(nums) - 1, which causes index errors or wraps around to nums[-1] in Python.
  • Moving toward the smaller neighbor. Only the side with the larger neighbor is guaranteed to contain a peak.
  • Missing the single-element case, where index 0 is a peak because both sides are treated as negative infinity.

Frequently asked questions

How can binary search work on an unsorted array?

Binary search only needs a way to discard half the range safely. If the middle element has a larger neighbor, a peak must exist on that side, so the other half can be dropped.

Why is there always a peak?

Both ends of the array are bordered by negative infinity and adjacent values differ. Starting anywhere and always stepping to a larger neighbor must stop somewhere, and the place where it stops is a peak.

Does the algorithm return the global maximum?

Not necessarily. It returns some local peak, which the problem accepts. Finding the global maximum would require looking at every element.

What is the time complexity of Find Peak Element?

It is O(log n) time and O(1) space, since every step halves the range that still contains a guaranteed peak.