Problem Description
Given an array of distinct integers, find all pairs of elements with the minimum absolute difference of any two elements. Each pair [a, b] must satisfy a < b and the pair's difference must match the smallest absolute difference found in the array. The final list should be in ascending order.
Key Insights
- Sorting the input array will allow efficient comparison of adjacent elements.
- The minimum absolute difference can only be among pairs of consecutive elements after sorting.
- Only one pass through the sorted array is needed to determine the minimum difference and collect the corresponding pairs.
Space and Time Complexity
Time Complexity: O(n log n) due to sorting the array. Space Complexity: O(n) for storing the sorted array and the final result.
Solution
The solution involves first sorting the input array. This sorting step ensures that the smallest differences are between consecutive elements. Next, make a single pass over the sorted array to compute the differences between adjacent pairs. Keep track of the minimum difference encountered. Then, iterate again (or concurrently) to collect all pairs that match this minimum difference. This approach uses sorting and a simple iteration, which minimizes the need for additional data structures beyond the final result list.