Gamified challenge · Binary search on the slope
heights[i] is the ridge's height at point i, and no two neighbours are equal. A peak is a point taller than each of its neighbours; beyond both ends the ground drops away forever, so the first or last point can be a peak too.
Write peak(heights) that returns the index of any peak. Several peaks may exist; every one of them is accepted. Try to check only about log2 n heights instead of walking the whole ridge.
Example: for [1, 2, 1, 3, 5, 6, 4] both 1 (height 2) and 5 (height 6) are right; for [1, 2, 3, 1] the only peak is 2.
This game is part of the paid plans. Sign in, then pick a plan to write your answer, watch it play out, and step through the algorithm with its guide.
Sign in with Google