Statement▼
Given an array of integers nums and an integer k, return the nums[i], nums[j]), where i j num.length.
The distance between a pair of integers,
a andb , is defined as the absolute difference between them.
Constraints:
n== nums.length2≤n≤103 0≤ nums[i]≤103 1≤ k≤2n×(n−1)
Note: Given an array of size
n , the total number of possible pairs is given bynC2 . AsnC2 evaluates to2n×(n−1) , there are exactly these much possiblek -distances.
Solution
We use a combination of binary search and the sliding window technique to develop an optimal solution to this problem. First, we sort the array and then apply binary search to find the k. When the binary search range converges, we get the
Given a distance d, the helper function counts the number of pairs with a distance d. It uses the sliding window technique with two pointers, left and right. The right pointer iterates through the array and the left pointer increments only when the difference between the elements at the two pointers exceeds d. The number of valid pairs is calculated using right - left, as the pointers are maintained such that the difference between the two elements is always d. If the difference between the elements at right and left is d, then the difference between any two elements within the range bounded by these pointers will also be d because the array is sorted.
Using the above intuition, we implement the smallest_distance_pair function as follows:
The first step is to sort the array
nums.Sorting helps calculate pairwise distances in increasing order easily. After sorting, the smallest distance between any two elements will always be between adjacent elements.
Define the binary search range using the variables
lowandhigh:Initialize
lowwith0.Initialize
highwithnums[nums.length - 1] - nums[0]representing the largest possible distance between the smallest and largest elements.
Use binary search to find the smallest distance. The search continues as long as the
lowis smaller than thehigh:Calculate the midpoint,
mid, which represents a potential candidate for thekth smallest distance. The goal is to find the smallest distance for which there are at leastkpairs.For each midpoint
mid, we count how many pairs have a distance less than or equal tomidusing the helper functioncount_pairs_with_distance.After counting the pairs, adjust the binary search range:
If the count is less than k, we increase
lowtomid + 1.If the count is greater than or equal to k, we decrease
hightomid.
Return
lowas thekth smallest distance when the binary search terminates.
The helper function count_pairs_with_distance uses the sliding window technique to count the number of valid pairs. It takes the value mid from the smallest_distance_pair function as its parameter (denoted as d) and is implemented as follows:
Initialize
countwith0.Initialize two pointers,
leftandright, with0.Iterate through the array using the
rightpointer and check how many pairs with the currentrightelement have a distance less than or equal tod.While
nums[right] - nums[left] > d, it means the pair (nums[left], nums[right]) is too far apart, so theleftpointer is incremented to reduce the distance.The number of valid pairs with the
rightis thenright - left, which is added tocount.
Let’s look at the following illustration to get a better understanding of the solution: