
If ‘NUMS’ = [1, 2, 3] then all possible pairs distances are:
Index 0 and 1 i.e abs(‘NUMS[0]’ - ‘NUMS[1]’) = 1
Index 0 and 2 i.e abs(‘NUMS[0]’ - ‘NUMS[2]’) = 2
Index 1 and 2 i.e abs(‘NUMS[1]’ - ‘NUMS[2]’) = 1
The first line contains a single integer ‘T’ representing the number of test cases.
The first line of each test case will contain two single space-separated integers ‘N’ and ‘K’ which represent the size of ‘NUMS’ and the ‘K-th’ smallest distance that “Ninja” has to find.
The next line contains ‘N’ space-separated integers representing the values of the given array/list ‘NUMS’.
For each test case, print a single line containing a single integer denoting the ‘K-th’ smallest distance among all pairs of the ‘NUMS’.
The output for every test case will be printed in a separate line.
You don’t need to print anything; It has already been taken care of. Just implement the given function.
1 <= ‘T’ <= 50
2 <= ‘N’ <= 10000
1 <= ‘K’ <= (N *(N - 1)) / 2
0 <= ‘NUMS[i]’ <= 100000
Where ‘T’ is the number of test cases, 'N' is the size of ‘NUMS’, ‘K’ represents the ‘K-th’ smallest distance among all pairs of the ‘NUMS’ and ‘NUMS[i]’ represents the ‘i-th’ value of ‘NUMS’.
Time limit: 1 sec.
The basic idea is to run two loops and find all possible pair distances and store them in a list/array ‘allPairDist’. Then sort this ‘allPairDist’ and return the value present at ‘K’ - 1 index.
First, we sort the array/list ‘NUMS’. Then we are applying binary search and inside this binary search, we are checking how many pairs with distance less than equal to mid.
The steps are as follows: