Minimum Absolute Difference

Given an array of distinct integers arr, find all pairs of elements with the minimum absolute difference of any two elements. Return a list of pairs in ascending order (with respect to pairs), each pair [a, b] follows: a, b are from arr, a < b, and b - a equals the minimum absolute difference of any two elements in arr.

30:00

Minimum Absolute Difference
easy
Free written solution
Topics
Companies

Given an array of distinct integers arr, find all pairs of elements with the minimum absolute difference of any two elements. Return a list of pairs in ascending order (with respect to pairs), each pair [a, b] follows: a, b are from arr, a < b, and b - a equals the minimum absolute difference of any two elements in arr.

Example 1:
Input: {"arr":[4,2,1,3]}
Output: [[1,2],[2,3],[3,4]]
Constraints:
  • 2≤arr.length≤1052 \leq \text{arr.length} \leq 10^5

  • −106≤arr[i]≤106-10^6 \leq \text{arr}[i] \leq 10^6

  • All values in arr are distinct.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"arr":[4,2,1,3]}

The closest pair of numbers must be neighbours once the array is sorted, so sort first and only compare adjacent values.

best gap

∞

this gap

-

4213unsorted: neighbours here say nothing yet

result (0)

empty

Variables
VariableValue
best∞
len(result)0
DepthFunction Call
Stack empty
0/5