Given an array of distinct integers arr, find all pairs of elements with the minimum absolute difference between them.
Return a list of pairs in ascending order (with respect to pairs). Each pair [a, b] must satisfy the following conditions:
-
a
and
b
are elements from
arr
.
-
a < b
.
-
b - a
equals the minimum absolute difference of any two elements in
arr
.
Function Signature
def minimum_abs_difference(arr: list[int]) -> list[list[int]]:
Rules
-
arr
is not necessarily sorted.
-
Each pair is written as
[smaller, larger]
.
-
The list of pairs is sorted ascending by the first element. Because the values are distinct, no two pairs share a first element, so this order is unique.
Constraints
-
2 <= arr.length <= 10⁵
-
-10⁶ <= arr[i] <= 10⁶
-
All integers in
arr
are distinct.
Examples
Input: arr = [4,2,1,3]
Output: [[1,2],[2,3],[3,4]]
Sorted, the array is [1,2,3,4]. The minimum difference is 1, and each of the three neighboring pairs has that difference.
Input: arr = [1,3,6,10,15]
Output: [[1,3]]
The gaps between neighbors are 2, 3, 4 and 5. The minimum is 2, so the only pair is [1,3].