528. Find K-th Smallest Pair Distance

HardBinary SearchBinary Search on AnswerTwo Pointers

Given an array, the distance of a pair is the absolute difference of its two elements. Return the k-th smallest distance among all pairs. Use binary search on the distance with a two-pointer count. Input: '[arr], k'.

Input: '[arr], k'.

Output: Integer — the k-th smallest pair distance.

Examples

Example 1
Input: [1,3,1], 1
Output: 0
Explanation: Smallest distance is 0 (the two 1s).
Example 2
Input: [1,1,1], 2
Output: 0
Explanation: All pairs have distance 0.
Example 3
Input: [1,6,1], 3
Output: 5
Explanation: 3rd smallest distance is 5.

Constraints

Asked by

AmazonGoogleBloombergMicrosoft
Solve this problem in the editor →