Peak Element Visualization & Animation

Finds any element strictly greater than its neighbors using binary search; O(log n) even on unsorted arrays.

## What is it? A peak element is one that is strictly greater than its neighbors. Find any such element in an array where no two adjacent elements are equal. Binary search works because the problem guarantees a peak always exists. ## How it works - If `arr[mid] > arr[mid+1]` → a peak exists in the left half (including `mid`); set `high = mid` - Else → `arr[mid+1] > arr[mid]`, so a peak exists in the right half; set `low = mid + 1` - When `low == high`, that index is a peak ## When to use - Any "find a local maximum" problem on an array - Optimization problems where you only need one valid peak (not all) ## Key Points - O(log n) — binary search is applicable because if `arr[mid] < arr[mid+1]`, the right side must have a peak (guaranteed by boundaries) - Works even on arrays that are not sorted overall - Returns any one peak; multiple peaks may exist

Category: algorithms

Difficulty: intermediate

Time Complexity: O(log n)

Space Complexity: O(1)

Peak Element

intermediate

Finds any element strictly greater than its neighbors using binary search; O(log n) even on unsorted arrays.

PhaseInit
Mid
Peak
lo
mid
hi
10
[0]
20
[1]
15
[2]
2
[3]
23
[4]
90
[5]
80
[6]
Current mid being tested
Peak element found
Active search window
Outside search space / eliminated
Find a peak in [10, 20, 15, 2, 23, 90, 80]. lo=0, hi=6.