Problem · Array
Minimum Absolute Difference Pairs
Learn this problemProblem statement
You are given an array latencies of n distinct integers representing API response times.
Your objective is to identify all pairs of response times such that:
- The absolute difference between the two values is the smallest possible among all pairs in the array.
- Within each pair, the first value is smaller than the second.
- The final list of pairs is sorted in ascending order based on the values.
Return all such pairs.
Function
minimumAbsoluteDifferencePairs(latencies: int[]) → int[][]Examples
Example 1
latencies = [6, 2, 4, 10]return = [[2, 4], [4, 6]]Input: n = 4, latencies = [6, 2, 4, 10]
Output: [[2, 4], [4, 6]]
Explanation: The minimal absolute difference is 2, and the pairs with that difference are (2,4) and (4,6). Within the pairs, the elements are ordered, and then the pairs themselves are in ascending order.
Example 2
latencies = [4, -2, -1, 3]return = [[-2, -1], [3, 4]]Input: n = 4, latencies = [4, -2, -1, 3]
Output: [[-2, -1], [3, 4]]
The minimal absolute difference is 1, and the pairs with that difference are (-2, -1) and (3, 4).
Constraints
2 ≤ n ≤ 10^5-2 × 10^6 ≤ latencies[i] ≤ 2 × 10^6- The
latenciesarray contains no duplicate elements. - Test Case Input Format
- The first line contains an integer
n, the size of thelatenciesarray. - Each of the next
nlines contains an integer,latencies[i].
- The first line contains an integer