Problem Statement
Find Peak Element
A peak element is a number that is strictly bigger than the number on its left and the number on its right. You are given a 0-indexed array of integers called nums. Your job is to find any one peak and return its index (its position in the array). If there is more than one peak, returning any of them is fine. To make the edges easy to handle, imagine that just outside the array, on both ends, the values are negative infinity (the smallest number possible). That means the very first or very last element only has to beat its single real neighbor to count as a peak.
Signals to notice
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.
Trace it on nums=[1,2,3,1]
init: left=0, right=3 mid=(0+3)//2=1; nums[1]=2 < nums[2]=3 -> uphill, go right: left=2 now left=2, right=3; mid=(2+3)//2=2; nums[2]=3 < nums[3]=1? no -> downhill, go left: right=2 left==right==2 -> loop ends return left = 2 (nums[2]=3 is a peak)
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.
Shape of the loop
left, right = 0, n-1
while left < right:
mid = (left + right) // 2
if nums[mid] < nums[mid+1]: left = mid + 1 # peak is to the right
else: right = mid # peak is at mid or left
return leftPseudocode only — the full worked solution lives in the Solution tab.
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.