Find K-th Smallest Pair Distance

Try to solve the Find K-th Smallest Pair Distance problem.

Statement

Given an array of integers nums and an integer k, return the kthk^{th} smallest distance between any pair of integers (nums[i], nums[j]), where 00 \leq i << j << num.length.

The distance between a pair of integers, aa and bb, is defined as the absolute difference between them.

Constraints:

  • n==n == nums.length

  • 2n1032 \leq n \leq 10^3

  • 00 \leq nums[i] 103\leq 10^3

  • 11 \leq k n×(n1)2\leq \frac{n \times (n-1)}{2}

Note: Given an array of size nn, the total number of possible pairs is given by nC2{}^{n}C_{2}. As nC2{}^{n}C_{2} evaluates to n×(n1)2\frac{n \times (n-1)}{2}, there are exactly these much possible kk-distances.

Examples

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.