← Visualizers
Binary Search — Hard LeetCode 162

Find Peak Element / visualized

No target, no need for the whole array to be sorted — just follow the slope. Compare nums[mid] to its right neighbor: if it's still climbing, a peak is guaranteed somewhere ahead (imagine the array bounded by -∞ on both edges — an ascending run can't just fall off the end). If it's flat or descending, the peak is at mid or to its left.

Execution

idle
Press Run to begin.
0 / 0
Speed

Java · running line

left mid right peak
O(log n) time · O(1) space  ·  works even on an unsorted array — a peak is guaranteed to exist for any array with imaginary -∞ edges.