Collect All Closest Adjacent Value Pairs
Heaps, Sorting & SearchAuto-graded · 21 test casesLast updated
Tests sorting and greedy scanning to identify the smallest gap between elements and extract all pairs achieving it.
You are given an integer array nums which may be unsorted and may contain duplicate values.
Your task is to identify the smallest absolute difference between any two elements at distinct positions in the array, then return all pairs of values that achieve this smallest difference.
Each pair must be formatted as [smaller_value, larger_value] (the smaller value comes first). The list of pairs should be sorted in ascending order: first by the smaller value, then by the larger value if needed.
Constraints
0 ≤ len(nums) ≤ 10⁵-10⁶ ≤ nums[i] ≤ 10⁶- If fewer than two elements exist, return an empty list
- Elements are compared by position, not by value (so duplicate values at different positions form valid pairs)
Examples
Example 1
Input: closest_value_pairs([-1, -2, -4, -5, 7, 10])
Output: [[-5, -4], [-2, -1]]
Hints
Hint 1
After sorting the array, which pairs of elements are guaranteed to have the smallest possible differences?
Hint 2
The minimum difference can only occur between adjacent elements in the sorted array—non-adjacent pairs will always have a larger or equal gap.
Hint 3
Once you find the minimum gap, a second pass through adjacent pairs collects all matches.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The core insight is that the minimum absolute difference between any two values can only occur between consecutive elements in a sorted array.
Why? Consider a sorted array and any two non-adjacent positions i and j where j > i + 1. The difference can be expressed as a sum of consecutive gaps:
sorted[j] - sorted[i] = (sorted[i+1] - sorted[i]) + (sorted[i+2] - sorted[i+1]) + ... + (sorted[j] - sorted[j-1])
Each term in this sum is non-negative (the array is sorted), so the total is at least as large as any single term. Therefore, a non-adjacent pair cannot have a smaller difference than the smallest consecutive gap.
This observation gives us a clean two-pass algorithm:
- Sort the input array to bring close values next to each other
- Scan all adjacent pairs to find the minimum difference
- Collect all adjacent pairs whose difference equals that minimum
The sorting step also handles the output requirements automatically: each pair [sorted[i], sorted[i+1]] is already ordered as [smaller, larger], and pairs are generated left-to-right in sorted order.
Edge Cases
- Empty or single-element arrays: No pairs can be formed, return
[] - Duplicate values: When the array contains duplicates, the minimum difference is
0, and adjacent identical values form valid pairs - All differences equal: Every consecutive pair gets included in the result
- Negative numbers: Subtraction on sorted values automatically handles signs correctly
Complexity
- Time: O(n log n) dominated by sorting. The two linear scans add O(n) which is absorbed.
- Space: O(n) for the sorted copy and the output list. In the worst case (all consecutive gaps are equal), the output contains n-1 pairs.
Solution
from typing import List
def closest_value_pairs(nums: List[int]) -> List[List[int]]:
if len(nums) < 2:
return []
sorted_nums = sorted(nums)
smallest_gap = min(sorted_nums[i + 1] - sorted_nums[i]
for i in range(len(sorted_nums) - 1))
result = []
for i in range(len(sorted_nums) - 1):
if sorted_nums[i + 1] - sorted_nums[i] == smallest_gap:
result.append([sorted_nums[i], sorted_nums[i + 1]])
return result