Problem · Array
Minimum Height Difference Between Distant Peaks
Learn this problemProblem statement
A mountaineer is studying a line of mountain peaks numbered from 0 onward. You are given their heights in meters as an integer array heights and a minimum viewing distance viewingGap.
The mountaineer may compare peaks at indices a and b only when |a - b| >= viewingGap.
Return the minimum possible value of |heights[a] - heights[b]| over all comparable pairs.
Function
minimumPeakHeightDifference(heights: int[], viewingGap: int) → intExamples
Example 1
heights = [1,5,4,20,9]viewingGap = 3return = 4The comparable index pairs are (0, 3), (0, 4), and (1, 4). Their height differences are 19, 8, and 4, so the minimum is 4.
Example 2
heights = [3,20,5,8]viewingGap = 1return = 2Every pair of distinct indices is comparable. Peaks at indices 0 and 2 have heights 3 and 5, giving the minimum difference 2.
Example 3
heights = [7,12,7]viewingGap = 2return = 0The only comparable pair is at indices 0 and 2. The two heights are equal, so their difference is 0.
Constraints
2 <= heights.length <= 10^50 <= heights[i] <= 10^91 <= viewingGap < heights.length
More Tiktok problems
- Concatenate Digit-wise SumsOA · Seen Aug 2026
- Count 2x2 Submatrices by Black CellsOA · Seen Aug 2026
- Count House Segments After DestructionOA · Seen Aug 2026
- Debugger Breakpoint ActionsOA · Seen Aug 2026
- Distribution Center Package AllocationOA · Seen Aug 2026
- Find All Local PeaksOA · Seen Aug 2026
- Minimum Operations for Stepwise StructuresOA · Seen Aug 2026
- Rating Level After ChangesOA · Seen Aug 2026