Problem. A peak is an element strictly greater than its neighbours. Given nums where no two adjacent values
are equal, return the index of any peak, in O(log n). Treat nums[−1] and nums[n] as −∞, so the ends count if
they beat their single neighbour.
[1, 2, 3, 1] → 2
[1, 2, 1, 3, 5, 6, 4] → 1 or 5
[5, 4, 3] → 0
The array is not sorted. Binary search still works, and seeing why is the point of this lesson: you don't need sorted data, you need a rule that always tells you which half is guaranteed to contain an answer.
ML context. Validation accuracy as a function of one hyperparameter (learning rate on a log grid, regularisation strength, number of trees) is often single-peaked: it rises, then falls. Each point costs a training run. Finding the best point with a slope-based binary search needs about log₂ n runs instead of n. The same peak idea locates the best threshold on a smooth F1 curve, or a local maximum in a signal.