Google Big Code
A geological survey team is analyzing a mountain range represented as an array of N elevation measurements at equally-spaced points along a ridge. A peak is defined as a position i (1 <= i <= N-2, 0-indexed) where the elevation is strictly greater than both its immediate neighbors:
The first and last positions (indices 0 and N-1) cannot be peaks by definition.
The team can perform elevation adjustment operations — each operation changes the elevation at any single position by exactly 1 unit, either up or down. Each unit of change costs exactly 1 operation regardless of direction. Given a budget of K operations, the team wants to transform the elevation array into a valid configuration where at least one peak of height H exists, maximizing H.
The evaluation of forming a peak at a position can be done independently of other positions. For a chosen position, the cost is the total adjustments required to satisfy the peak condition at that position. Adjustments made for one position do not need to be consistent with those for another.
The feasibility of forming a peak at any position depends only on that position and its two immediate neighbors — the final configuration must satisfy the peak condition simultaneously for the chosen position and both its neighbors.
If a peak of height H is achievable within K operations, then any height less than H is also achievable. Return 0 if no valid peak can be created even with K operations.