> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Find K-th Smallest Pair Distance

> Tested Python solution for LeetCode 719 with 14 pytest cases. Generate a practice environment with lcpy.

LeetCode 719, [Hard](/catalog/hard). Topics: [Array](/catalog/topics/array), [Two Pointers](/catalog/topics/two-pointers), [Binary Search](/catalog/topics/binary-search), [Sorting](/catalog/topics/sorting). [View on LeetCode](https://leetcode.com/problems/find-k-th-smallest-pair-distance/description/).

Generate this problem as a practice environment: tested reference solution, 14 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 719   # by problem number
lcpy gen -s find_k_th_smallest_pair_distance   # by problem name
```

## Problem

The distance of a pair of integers `a` and `b` is defined as the absolute difference between `a` and `b`.

Given an integer array `nums` and an integer `k`, return the `kth` smallest distance among all the pairs `nums[i]` and `nums[j]` where `0 <= i < j < nums.length`.

### Examples

```
Input: nums = [1,3,1], k = 1
Output: 0
Explanation: Here are all the pairs:
(1,3) -> 2
(1,1) -> 0
(3,1) -> 2
Then the 1st smallest distance pair is (1,1), and its distance is 0.
```

```
Input: nums = [1,1,1], k = 2
Output: 0
```

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

### Constraints

* n == nums.length
* 2 \<= n \<= 10^4
* 0 \<= nums\[i] \<= 10^6
* 1 \<= k \<= n \* (n - 1) / 2

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/find_k_th_smallest_pair_distance/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/find_k_th_smallest_pair_distance/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from bisect import bisect_right


class Solution:
    # Time: O(n log n + n log W) where W = max(nums) - min(nums)
    # Space: O(n)
    def smallest_distance_pair(self, nums: list[int], k: int) -> int:
        nums = sorted(nums)
        n = len(nums)

        def count_pairs_within(dist: int) -> int:
            count = 0
            for i in range(n):
                count += bisect_right(nums, nums[i] + dist, lo=i + 1) - (i + 1)
            return count

        low, high = 0, nums[-1] - nums[0]
        while low < high:
            mid = (low + high) // 2
            if count_pairs_within(mid) >= k:
                high = mid
            else:
                low = mid + 1
        return low
```

## Complexity

| Time | Space |
| - | - |
| O(n log n + n log W) where W = max(nums) - min(nums) | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.