Skip to content

visual walkthrough

Find Peak Element

MediumBinary Search on SlopeReported at: MetaAmazonGoogle+4

Binary search without sorted data, guided by the slope.

Solve on LeetCode

The idea

Any peak will do. Picture the edges as holes (−∞): walking uphill from anywhere must end at a peak.

So look at the slope at mid and keep the uphill side. That side always contains a peak.

Complexity

approachtimespace
Walk until it dropsO(n)O(1)
Binary search on the slopeO(log n)O(1)

More walkthroughs