Skip to content
BytePatterns

Peak In Bumpy List

MediumSearching#binary-search#slope-following~30m

Problem

A peak is a position whose value is greater than both of its neighbours, where a missing neighbour off either end counts as smaller than anything. Given a non-empty list in which no two neighbouring values are equal, return the position of any peak. The list is not sorted, and several peaks may exist.

Examples

Input:  nums = [1, 2, 3, 1]
Output: 2
Why:    the value 3 stands above both of its neighbours
Input:  nums = [1, 2, 1, 3, 5, 6, 4]
Output: 5
Why:    position 1 is also a peak, and either answer is acceptable
Input:  nums = [1]
Output: 0
Why:    edge case, a lone value has two imaginary smaller neighbours

Hints

0 / 3

Stuck on the idea rather than the code? Find a Peak covers it.