LeetCode ยท #162

Find Peak Element Solution

Find any index i such that nums[i] is strictly greater than its neighbours. Return its index.

01
Section One ยท Problem

Problem Statement

๐Ÿท๏ธ

Difficulty

Medium

๐Ÿ”—

LeetCode

Problem #162

๐Ÿ—๏ธ

Pattern

Binary Search โ€” hill climbing

A peak element is strictly greater than its neighbours. Given an integer array nums, find a peak element and return its index. If the array contains multiple peaks, return the index of any peak. You may imagine that nums[-1] = nums[n] = -โˆž. You must write an O(log n) algorithm.

Example
Input: nums = [1,2,3,1] Output: 2 // nums[2] = 3 is a peak (3 > 2 and 3 > 1)
Constraints
โ€ข 1 โ‰ค nums.length โ‰ค 1000 โ€ข -2ยณยน โ‰ค nums[i] โ‰ค 2ยณยน - 1 โ€ข nums[i] โ‰  nums[i + 1] for all valid i (no adjacent equals)
02
Section Two ยท Approach 1

Linear Scan โ€” O(n)

Walk through the array. Return the first index where nums[i] > nums[i+1] โ€” that element is a peak (it's already greater than nums[i-1] because we came from an ascending sequence).

Problem: O(n). Since no two adjacent elements are equal, the array is a series of slopes. Binary search always moves toward a higher neighbour, guaranteeing a peak in O(log n).
MetricValue
TimeO(n)
SpaceO(1)
03
Section Three ยท Approach 2

Binary Search โ€” O(log n)

Compare nums[mid] to nums[mid + 1]. If mid is on an ascending slope (nums[mid] < nums[mid+1]), a peak must exist to the right. If mid is descending, a peak must exist to the left (or mid itself). Move toward the higher side.

๐Ÿ’ก Mental model: Imagine hiking in fog and you can only see one step ahead. If the terrain is going up to your right, walk right โ€” there must be a summit somewhere (the boundary is -โˆž). If it's going down, walk left. You're guaranteed to find a peak because the edges are negative infinity โ€” you can't walk off into endlessly ascending terrain.
Algorithm
  • Step 1: left = 0, right = n - 1.
  • Step 2: While left < right:
  • Step 3: If nums[mid] < nums[mid + 1] โ†’ ascending โ†’ peak is right: left = mid + 1.
  • Step 4: Else โ†’ descending โ†’ peak is left or at mid: right = mid.
  • Step 5: Return left.
๐ŸŽฏ Why is a peak guaranteed?:
  • Since nums[-1] = nums[n] = -โˆž, if you follow an ascending slope, it must eventually descend (boundary is -โˆž).
  • The transition from ascending to descending is a peak. Binary search finds it by always moving uphill.
04
Section Four ยท Trace

Visual Walkthrough

Trace through nums = [1, 2, 1, 3, 5, 6, 4]:

Binary Search โ€” move toward the higher neighbour
nums: [1, 2, 1, 3, 5, 6, 4] Step 1: L=0,R=6,mid=3 โ†’ nums[3]=3 < nums[4]=5 โ†’ ascending โ†’ L=4 Step 2: L=4,R=6,mid=5 โ†’ nums[5]=6 > nums[6]=4 โ†’ descending โ†’ R=5 Step 3: L=4,R=5,mid=4 โ†’ nums[4]=5 < nums[5]=6 โ†’ L=5 L==R==5 โ†’ peak at index 5 (value 6) Answer: 5 โœ“
05
Section Five ยท Implementation

Code โ€” Java & Python

Java โ€” Binary Search
class Solution { public int findPeakElement(int[] nums) { int left = 0, right = nums.length - 1; while (left < right) { int mid = left + (right - left) / 2; if (nums[mid] < nums[mid + 1]) left = mid + 1; // ascending โ€” peak is right else right = mid; // descending โ€” peak is left or here } return left; } }
Python โ€” Binary Search
class Solution: def findPeakElement(self, nums: list[int]) -> int: left, right = 0, len(nums) - 1 while left < right: mid = (left + right) // 2 if nums[mid] < nums[mid + 1]: left = mid + 1 else: right = mid return left
06
Section Six ยท Analysis

Complexity Analysis

ApproachTimeSpaceTrade-off
Linear ScanO(n)O(1)Finds first descent point
Binary Search โ† optimal O(log n) O(1) Move uphill โ€” peak guaranteed by -โˆž boundaries
07
Section Seven ยท Edge Cases

Edge Cases & Pitfalls

CaseInputWhy It Matters
Single element[1]Always a peak (both neighbours are -โˆž). L==R==0.
Ascending[1,2,3,4]Peak is at the end (index 3). Binary search goes right every time.
Descending[4,3,2,1]Peak is at the start (index 0). Binary search goes left every time.
Multiple peaks[1,3,1,5,1]Return any peak โ€” both index 1 and 3 are valid.
โš  Common Mistake: Comparing nums[mid] to nums[mid - 1] instead of nums[mid + 1]. When mid = 0, accessing nums[-1] is out of bounds. Comparing to mid + 1 is safe because the loop condition left < right ensures mid < right โ‰ค n-1, so mid + 1 is always valid.

โ† Back to Binary Search problems