Ninja has been given a ‘NUMS’ array/list of size ‘N’. ‘NUMS’ array/list contains ‘N’ positive integers. Ninja has to find the ‘K-th’ smallest distance among all pairs of the ‘NUMS’.
Note: The distance between any two numbers of ‘NUMS’ is abs(‘NUMS[i]’ - ‘NUMS[j]’).
For example:
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’.
Output Format:
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.
Note:
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.
2
2 1
1 2
4 4
1 2 3 4
1
2
In the first test case, all possible pairs distances are:
Index 0 and 1 i.e abs(‘NUMS’[0] - ‘NUMS’[1]) = 1.
Hence the 1’st smallest distance is 1.
In the first test case, 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 0 and 3 i.e abs(‘NUMS’[0] - ‘NUMS’[3]) = 3
Index 1 and 2 i.e abs(‘NUMS’[1] - ‘NUMS’[2]) = 1
Index 1 and 3 i.e abs(‘NUMS’[1] - ‘NUMS’[3]) = 2
Index 3 and 4 i.e abs(‘NUMS’[3] - ‘NUMS’[4]) = 1
So all possible distances in increasing order are 1 1 1 2 2 3.
Hence the 4’th smallest distance is 2.
2
5 3
1 1 2 2 3
4 2
3 2 100 8
1
5
In the first test case, all possible distances in increasing order are 0 0 1 1 1 1 1 1 2 2
Hence the 3’th smallest distance is 1.
In the second test case, all possible distances in increasing order are 1 5 6 92 97 98
Hence the 2’th smallest distance is 5.
Can you think of storing all the possible pair distances?
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.
The steps are as follows:
O((N ^ 2) * log(N)), where ‘N’ is the size of ‘NUMS’.
Since we are finding all possible pair distances using a nested loop in O(N^2) time and then using a sort function that takes O((N^2)*log(N^2)) time. Hence, the overall time complexity is O((N^2)*log(N)).
O(N ^ 2), where ‘N’ is the size of ‘NUMS’.
Because we are storing all possible pair distances of ‘NUMS’ in ‘allPairDist’. Hence the space complexity is O(N ^ 2).