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: beginner

Time Complexity: O(log n)

Space Complexity: O(1)

Peak Element

beginner

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