VFlash: Fast, Accurate Visual Planning via Flash Primitive Decoding
u_Unrolled
DSA for AI/ML · Module 05: Binary search

Lesson 5.7 of 10 in this module Unrolled Pro

Find Peak Element

Return the index of any element larger than its neighbours, in O(log n), on an array that isn't sorted. From checking every position, to the first downward step, to binary search on the slope: walk uphill and a peak must exist ahead. Then plateaus (equal neighbours), peaks in a 2D grid, finding the best hyperparameter on a single-peaked validation curve with few evaluations, and why the global maximum still needs O(n).

about 10 min 4 Python examples 2 quick checks

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.

text
[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.


1. Step 1: Clarify

That's the preview. Modules 00–01 are free; this lesson is part of Unrolled Pro.
Staff AI & Pro Tier Lesson

Unlock "Find Peak Element"

The foundations are free forever. Unrolled Pro unlocks the complete lesson, production architectures, and 55 battle-tested ML system design case studies.

Everything included with Pro:
All 234+ Deep-Dive Lessons across transformer internals, inference, RAG & agents
55 Staff-Level ML & System Design Case Studies with mathematical capacity estimates
AI Engineer Sheet & Exercises with worked solutions, self-checks, and code
100 Landmark Research Papers guided walkthroughs & progress tracking
14-Day Money-Back Guarantee — 100% full refund if you aren't satisfied
View Plans & Unlock All Lessons

From ₹167/mo (~$2/mo) · Cancel anytime · Instant activation

100% Risk-Free: 14-day no-questions-asked money-back guarantee