Skip to main content

Minimum Absolute Difference in Latency

easy
Software engineer

In distributed systems, network latency values are monitored to detect closely matched delays. You are given an integer array latencies representing a set of observed latency values.

Find all pairs of values in the array whose absolute difference equals the minimum absolute difference among all possible pairs.

Each pair [a, b] must satisfy a ≤ b, and the result must be returned as a list of pairs sorted in ascending order by the first element. Pairs are formed from two elements at different positions, so duplicate values can yield a pair with zero difference.

Example 1

Input

latencies = [4, 2, 6, 8]

Output

[[2, 4], [4, 6], [6, 8]]

Explanation

1. Sort the array: [2, 4, 6, 8].
2. Calculate differences between adjacent elements: (4-2)=2, (6-4)=2, (8-6)=2.
3. The minimum difference is 2.
4. All pairs with a difference of 2 are [2, 4], [4, 6], and [6, 8].

Example 2

Input

latencies = [1, 5, 6, 10]

Output

[[5, 6]]

Explanation

The minimum absolute difference is 1 (between 5 and 6).

Example 3

Input

latencies = [3, 7]

Output

[[3, 7]]

Explanation

Only one pair exists, so its difference is the minimum.

Constraints

  • 2 <= latencies.length <= 10⁵
  • 0 <= latencies[i] <= 10⁹

Onsite

ArraySorting
Language
Code editor loads in the browser.

Output

Input

[4,2,6,8]

Expected

[[2,4],[4,6],[6,8]]

Your output

Run to see your output.