mediumBinary SearchBinary Search

Find Peak Element

mediumTime: O(log n)Space: O(1)

Signals to notice

find ANY peak elementneighbors comparisonthe prompt wants you to keep cutting the search space down

Brute force first

Scan left to right, return first element greater than its neighbors. Simple but doesn't exploit the structure. That instinct is useful because it follows the prompt literally, but it usually keeps revisiting work the problem is begging you to organize.

The key insight

Binary search: if nums[mid] < nums[mid+1], a peak must exist to the right (the array goes up). If nums[mid] < nums[mid-1], a peak must exist to the left. Go toward the higher neighbor. The goal is not to be clever for its own sake, but to remember the one relationship that keeps the solution grounded as you move forward.

What must stay true

Moving toward the higher neighbor guarantees you'll eventually reach a peak. The array boundaries are -∞, so any upward trend must peak somewhere before going out of bounds. If that remains true after every update, the rest of the reasoning has a stable place to stand.

Easy way to go wrong

Thinking you need to find THE peak — you only need ANY peak. Also, comparing with both neighbors at once is unnecessary; comparing with just mid+1 is enough to decide direction. Most mistakes here are not about syntax; they come from losing track of what your state, pointer, or structure is supposed to mean.

Binary Search Pattern