1
+ '''
2
+ Given an array of distinct integers arr, find all pairs of elements with the minimum absolute difference of any two elements.
3
+
4
+ Return a list of pairs in ascending order(with respect to pairs), each pair [a, b] follows
5
+
6
+ a, b are from arr
7
+ a < b
8
+ b - a equals to the minimum absolute difference of any two elements in arr
9
+
10
+
11
+ Example 1:
12
+
13
+ Input: arr = [4,2,1,3]
14
+ Output: [[1,2],[2,3],[3,4]]
15
+ Explanation: The minimum absolute difference is 1. List all pairs with difference equal to 1 in ascending order.
16
+ Example 2:
17
+
18
+ Input: arr = [1,3,6,10,15]
19
+ Output: [[1,3]]
20
+ Example 3:
21
+
22
+ Input: arr = [3,8,-10,23,19,-4,-14,27]
23
+ Output: [[-14,-10],[19,23],[23,27]]
24
+
25
+
26
+ Constraints:
27
+
28
+ 2 <= arr.length <= 10^5
29
+ -10^6 <= arr[i] <= 10^6
30
+ '''
31
+
32
+ class Solution (object ):
33
+ def minimumAbsDifference (self , arr ):
34
+ """
35
+ :type arr: List[int]
36
+ :rtype: List[List[int]]
37
+ """
38
+ if not arr :
39
+ return []
40
+
41
+ arr .sort ()
42
+ mindiff = arr [1 ] - arr [0 ]
43
+ for index in range (2 , len (arr )):
44
+ mindiff = min (mindiff , (arr [index ] - arr [index - 1 ]))
45
+
46
+ result = []
47
+ for index in range (1 , len (arr )):
48
+ if arr [index ] - arr [index - 1 ] == mindiff :
49
+ result .append ([arr [index - 1 ], arr [index ]])
50
+ return result
51
+
0 commit comments