Solution: Find K-th Smallest Pair Distance
Let’s solve the Find K-th Smallest Pair Distance problem using the Sort and Search pattern.
We'll cover the following
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,
and , is defined as the absolute difference between them.
Constraints:
nums.length
nums[i]
k
Note: Given an array of size
, the total number of possible pairs is given by . As evaluates to , there are exactly these much possible -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 smallestDistancePair
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
low
andhigh
:Initialize
low
with0
.Initialize
high
withnums[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
low
is smaller than thehigh
:Calculate the midpoint,
mid
, which represents a potential candidate for thesmallest distance. The goal is to find the smallest distance for which there are at least k
pairs.For each midpoint
mid
, we count how many pairs have a distance less than or equal tomid
using the helper functioncountPairsWithDistance
.After counting the pairs, adjust the binary search range:
If the count is less than k, we increase
low
tomid + 1
.If the count is greater than or equal to k, we decrease
high
tomid
.
Return
low
as thesmallest distance when the binary search terminates.
The helper function countPairsWithDistance
uses the sliding window technique to count the number of valid pairs. It takes the value mid
from the smallestDistancePair
function as its parameter (denoted as d
) and is implemented as follows:
Initialize
count
with0
.Initialize two pointers,
left
andright
, with0
.Iterate through the array using the
right
pointer and check how many pairs with the currentright
element 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 theleft
pointer is incremented to reduce the distance.The number of valid pairs with the
right
is thenright - left
, which is added tocount
.
Let’s look at the following illustration to get a better understanding of the solution:
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.