The first line of input contains a single integer T, representing the number of test cases or queries to be run.
Then the T test cases follow:
The first line of each test case contains two single space-separated integers 'N', and 'K', denoting the size of the array 'arr' and the distance respectively.
The second line of each test case contains 'N' single space-separated integers, elements of the array.
For each test case, print βtrueβ if the array contains any duplicate element within the 'K' distance from each other, otherwise, print "false".
Result for each test case will be printed in a separate line.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10^5
1 <= K <= 10^5
-10^9 <= arr[i] <= 10^9
Time Limit: 1sec
The naive approach is to run two loops, the outer loop will pick each element one by one.
The inner loop compares all elements which are within βKβ distance from and with the currently selected element.
If the selected element matches with any of the elements that we are comparing in the inner loop then it means there is a duplicate.
We need to check if the duplicate exists at a distance of βKβ.
We can use the sliding window technique. The idea is to process every window of size βKβ one at a time and store it into Set, now if the element is repeated in a window of size βKβ then we can say there is a duplicate within βKβ distance.