Problem · Array

Minimum Absolute Difference Pairs

Learn this problem
EasyAkuna Capital logoAkuna CapitalINTERNOA

Problem 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 latencies array contains no duplicate elements.
  • Test Case Input Format
    • The first line contains an integer n, the size of the latencies array.
    • Each of the next n lines contains an integer, latencies[i].

More Akuna Capital problems

drafts saved locally
public int[][] minimumAbsoluteDifferencePairs(int[] latencies) {
  // write your code here
}
latencies[6, 2, 4, 10]
expected[[2, 4], [4, 6]]
checking account