मुख्य कंटेंट तक स्किप करें
Back to ChallengesMinimum Absolute DifferenceEasy 15 min

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 to the minimum absolute difference.

Examples

Input: arr = [4,2,1,3]
Output: [[1,2],[2,3],[3,4]]
The minimum absolute difference is 1, and there are 3 pairs with difference 1.

Constraints

  • 2 <= arr.length <= 10^5
  • -10^6 <= arr[i] <= 10^6

Complexity Analysis

Time
O(N log N)
to sort the array.
Space
O(1) auxiliary, O(N) for the result array.

Test Cases

#1 Multiple pairs
Input: arr = [4,2,1,3]
Expected: [[1,2],[2,3],[3,4]]
#2 Single pair
Input: arr = [1,3,6,10,15]
Expected: [[1,3]]
Output
Click "Run Code" to see output here...