


You have been given an array “ARR” of integers and an integer ‘K’. Your task is to find a pair of distinct indices ‘i’ and ‘j’ such that ARR[i] == ARR[j] and | i - j | <= ‘K’.
The first line contains a single integer ‘T’ representing the number of test cases. The 'T' test cases follow.
The first line of each test case will contain two integers ‘N’ and ‘K’ where ‘N’ is the number of elements in the array, and the integer ‘K’ is described as above.
The second line of each test case will contain ‘N’ space-separated integers denoting the elements in the array.
Output Format :
For each test case, print “Yes” (without quotes) if a pair of such distinct indices (described in the problem statement) exists otherwise print “No” (without quotes).
Output for every test case will be printed in a separate line.
Note :
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 50
1 <= N <= 10000
1 <= K <= 10000
0 <= ARR[i] <= 10^9
Where 'ARR[i]' denotes the ith element of the given array.
Time limit: 1 sec
2
6 3
1 2 4 3 4 5
4 2
5 4 7 6
Yes
No
In the first test case, consider (2, 4) a pair of distinct indices where ARR[2] = ARR[4] and |2 - 4| = 2 which is less than or equal to ‘K’. So print “Yes”.
In the second test case, each element of the given array is distinct. So print “No”.
2
3 1
4 4 4
2 1
1 2
Yes
No
In the first test case, consider (0, 1) a pair of distinct indices where ARR[0] = ARR[1] and |0 - 1| = 1 which is less than or equal to ‘K’. So print “Yes”.
In the second test case, each element of the given array is distinct. So print “No”.
Can you think of considering all pairs of elements?
The basic idea of this approach is to iterate through all the possible pairs of indices and check if duplicate elements exist satisfying the given constraint in the problem statement.
Consider the following steps:
O(N^2), where ‘N’ is the number of elements in the given array/list.
Since we are trying to consider every pair of indices in the given array/list. So the overall time complexity will be O(N^2).
O(1)
Since we are not using any extra space other than the few variables. So the overall space complexity will be O(1).