Problem · Array

Minimum Height Difference Between Distant Peaks

Learn this problem
MediumTiktok logoTiktokINTERNOA
See Tiktok hiring insights

Problem 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) → int

Examples

Example 1

heights = [1,5,4,20,9]viewingGap = 3return = 4

The 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 = 2

Every 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 = 0

The only comparable pair is at indices 0 and 2. The two heights are equal, so their difference is 0.

Constraints

  • 2 <= heights.length <= 10^5
  • 0 <= heights[i] <= 10^9
  • 1 <= viewingGap < heights.length

More Tiktok problems

drafts saved locally
public int minimumPeakHeightDifference(int[] heights, int viewingGap) {
    // Write your code here.
}
heights[1,5,4,20,9]
viewingGap3
expected4
checking account