Find K-th Smallest Pair Distance

Hard

Description

Given an integer array nums, find the kth smallest distance among all pairs. The distance of a pair (a, b) is the absolute difference |a - b|.

Examples

Input:nums = [1,3,1], k = 1
Output:0
Explanation:

Pairs: (1,3)=2, (1,1)=0, (3,1)=2. Smallest is 0.

Input:nums = [1], k = 1
Output:1
Explanation:

Edge case with a single-element array.

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

All pairs and their distances: (1,6)=5, (1,1)=0, (1,4)=3, (6,1)=5, (6,4)=2, (1,4)=3. Sorted distances: [0,2,3,3,5,5]. The 3rd smallest is 3.

Constraints

  • 2 ≤ nums.length ≤ 10⁴

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!