ntree.ai
ntree.aiFind K-th Smallest Pair Distance

Find K-th Smallest Pair Distance

hardsolvedattempted

Given an integer array nums and an integer k, return the k-th smallest distance among all pairs nums[i], nums[j] with i < j.

The distance of a pair of integers a and b is |a - b|.

Example 1:

Input:  nums = [1, 3, 1], k = 1
Output: 0
Explanation: [1, 1] pair makes the smallest distance which is 0 .

Example 2:

Input:  nums = [1, 6, 1], k = 3
Output: 5

Constraints:

  • 2 <= length of nums <= 10^4
  • 0 <= nums[i] <= 10^6
def smallestDistancePair(nums: list[int], k: int) -> int:
pass
 

Press Visualise to step through your code.