Find K-th Smallest Pair Distance - Complete Solution Guide
Find K-th Smallest Pair Distance is LeetCode problem 719, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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 k th smallest distance among all the pairs nums[i] and nums[j] where 0 <= i < j < nums.length . Example 1: 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 1 st smallest distance pair is (1,1), and its distance is 0. Example 2: Input: nums = [1,1,1], k = 2 Output: 0 Example
Detailed Explanation
The problem asks us to find the k-th smallest distance among all possible pairs of numbers in a given integer array `nums`. The distance between a pair of numbers `a` and `b` is defined as the absolute difference `|a - b|`. The input consists of an integer array `nums` and an integer `k`. The output is the k-th smallest distance. We need to consider all possible pairs `(nums[i], nums[j])` where `0 <= i < j < nums.length`.
Solution Approach
The solution uses a combination of sorting and binary search. First, the input array `nums` is sorted. Then, a binary search is performed on the range of possible distances to find the k-th smallest distance. For each `mid` value during the binary search, we count the number of pairs with a distance less than or equal to `mid`. If the count is less than `k`, it means that the k-th smallest distance is greater than `mid`, so we search in the upper half. Otherwise, it means the k-th smallest distance is less than or equal to `mid`, so we search in the lower half. The `count_pairs` function efficiently calculates the number of pairs using a two-pointer approach.
Step-by-Step Algorithm
- Step 1: Sort the input array `nums` in ascending order.
- Step 2: Initialize `low` to 0 and `high` to `nums[-1] - nums[0]`. These represent the lower and upper bounds of the possible distances.
- Step 3: Perform binary search between `low` and `high` until `low` is equal to `high`.
- Step 4: In each iteration of the binary search, calculate the `mid` value as `low + (high - low) // 2`.
- Step 5: Use the `count_pairs` function to count the number of pairs with a distance less than or equal to `mid`.
- Step 6: If the `count` is less than `k`, it means that the k-th smallest distance is greater than `mid`, so update `low` to `mid + 1`.
- Step 7: Otherwise, it means that the k-th smallest distance is less than or equal to `mid`, so update `high` to `mid`.
- Step 8: When the loop terminates, `low` will be equal to `high`, and this value will be the k-th smallest distance. Return `low`.
Key Insights
- Insight 1: Sorting the input array allows us to use binary search to efficiently find the k-th smallest distance.
- Insight 2: The problem can be solved by using binary search on the possible range of distances. The lower bound is 0, and the upper bound is the difference between the largest and smallest numbers in the array.
- Insight 3: For a given distance `mid`, we need to efficiently count the number of pairs with a distance less than or equal to `mid`. This can be done in O(n) time using two pointers after sorting.
Complexity Analysis
Time Complexity: O(nlogn)
Space Complexity: O(1)
Topics
This problem involves: Array, Two Pointers, Binary Search, Sorting.
Companies
Asked at: Flipkart, Pinterest.