K-th smallest distance among pairs
Given a list nums of n >= 2 integers (values may repeat) and an integer k, look at every pair of positions i < j and its distance abs(nums[i] - nums[j]). There are n(n − 1)/2 distances, duplicates included. Return the k-th smallest of them, counting from 1 (1 <= k <= n(n − 1)/2).
For example, [1, 6, 3] has the distances 5, 2 and 3, so k = 1 gives 2. Aim for O(n log n + n log W), where W = max(nums) − min(nums): listing every pair is O(n²).
Besides the tests, one large input (2,000 values) with a ~300 ms limit (scaled to your device's speed): brute force won't pass.
Examples
kth_smallest_distance([1,6,3], 1)→2kth_smallest_distance([1,6,3], 3)→5kth_smallest_distance([4,4,9,1], 2)→3