Find K-th Smallest Pair Distance
Try to solve the Find K-th Smallest Pair Distance problem.
We'll cover the following
Statement
Given an array of integers nums
and an integer k
, return the nums[i]
, nums[j]
), where i
j
num.length
.
The distance between a pair of integers,
and , is defined as the absolute difference between them.
Constraints:
nums.length
nums[i]
k
Note: Given an array of size
, the total number of possible pairs is given by . As evaluates to , there are exactly these much possible -distances.
Examples
Understand the problem
Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:
What is the output if the following array and k = 8
are given as input?
nums = [7, 5, 3, 11, 2]
The input is invalid.
Figure it out!
We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.
Try it yourself
Implement your solution in the following coding playground.
public class Solution {public static int smallestDistancePair(int[] nums, int k) {// Replace this placeholder return statement with your codereturn -1;}}
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.