Problem Description
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, each pair [a, b] follows:
- a, b are from arr
- a < b
- b - a equals to the minimum absolute difference of any two elements in arr.
Key Insights
- The problem requires finding the minimum absolute difference between pairs of distinct integers.
- Sorting the array simplifies the process of finding minimum differences since adjacent elements will have the smallest differences.
- The output pairs should be in ascending order based on their values.
Space and Time Complexity
Time Complexity: O(n log n) due to the sorting step, where n is the number of elements in the array. The subsequent linear pass to find pairs takes O(n) time. Space Complexity: O(n) for storing the result pairs in the worst case.
Solution
To solve the problem, we can follow these steps:
- Sort the input array
arr
. This allows us to easily find adjacent elements with the smallest differences. - Initialize a variable to track the minimum absolute difference.
- Iterate through the sorted array to find the minimum difference between adjacent elements.
- Create a list to store pairs that have this minimum difference.
- Return the list of pairs.
The data structure used here is a simple array (or list) to store the results, and the algorithm primarily relies on sorting followed by a linear scan.