Smallest Absolute Difference Pairs
via2dbi
Problem Given an array of distinct integers, find the smallest absolute difference between any two elements, then print every pair of elements that achieves that difference.
Input / Output
- Input: an integer
n(the array size), followed bynintegers. - Output: one pair per line as
a bwith the smaller number first. Print every pair whose absolute difference equals the minimum, ordered ascending by the first element of the pair.
Constraints
- 2 <= n <= 10^5
- -10^6 <= numbers[i] <= 10^6
- All elements are distinct.
Example
Input: n = 4, numbers = [6, 2, 4, 10]
Output:
2 4
4 6
The minimum difference is 2, achieved by (2,4) and (4,6); (6,10) differs by 4 so it is not printed. A tie case: [1, 3, 5, 7] → min diff 2, so all of 1 3, 3 5, 5 7 are printed.
asked …