Find all pairs of values in an array whose absolute difference is as small as possible.
Given an integer array, identify the smallest absolute difference between any two elements. Return every pair of values that achieves that minimum difference, with each pair listed in ascending order.
A pair should be included only once, even if the same values appear multiple times in the array. The final collection of pairs should be ordered consistently by the pair values.
Input Format
- An integer array
nums. - The array may contain duplicates and negative numbers.
Output Format
- Return a list of pairs
[a, b]such that:a < b|a - b|is the minimum absolute difference among all pairs in the array
- The pairs may be returned in ascending lexicographic order.
Constraints
- Elements may be negative, zero, or positive.
- A valid solution should avoid checking every pair when possible.
Example 1
Input
nums = [4, 2, 1, 3]
Output
[[1,2],[2,3],[3,4]]
Explanation
After sorting, the array becomes [1,2,3,4]. The minimum absolute difference is 1, achieved by each adjacent pair.
Example 2
Input
nums = [1, 3, 6, 10, 15]
Output
[[1,3]]
Explanation
The smallest absolute difference is 2, and only the pair [1,3] achieves it.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.