← DiffPush

Find peak element

Baseline Binary Search · 1D Arrays O(log n) · O(1)

Locating a Stress Crest in a Load Profile

A structural sensor logs one load value per segment along a beam, and inspection must sample the most stressed segment - one whose load strictly exceeds both neighbours. Segments beyond the beam's ends are treated as carrying no load, so a segment only needs to beat the neighbours that exist. Any crest qualifies when several exist.

Input: An array nums of n integers.

Output: The index of any element strictly greater than its neighbours, where out-of-array neighbours count as negative infinity.

Constraints

Examples

Example 1

Input: {"nums":[1,2,3,1]}
Output: 2
The load rises to 3 at index 2 and drops afterwards, making that segment the crest.

Example 2

Input: {"nums":[1,2,1,3,5,6,4]}
Output: 5
Two crests exist (index 1 and index 5) and either is a correct answer; this profile reports 5.

Solve this in your browser →

Also on LeetCode ↗