Kth distance
Easy Accuracy: 31.65% Submissions: 5288 Points: 2

Given an unsorted array A of size N that may contain duplicates. Also given a number K which is smaller than size of array. Find if array contains duplicates within k distance.

Input:
The first line of input contains an integer T denoting the number of test cases. The first line of each test case is K and N, K is the distance and N is the size of array. The second line of each test case contains N elements.

Output:
Print "True" if array contains duplicates within k distance else False.

Constraints:
1 ≤ T ≤ 100
1 ≤ N ≤ 106
1 ≤ K ≤ N
1 ≤ A[i] ≤ 107

Example:
Input:

4
3 8
1 2 3 4 1 2 3 4
3 6
1 2 3 1 4 5
3 5
1 2 3 4 5
3 5
1 2 3 4 4

Output:
False
True
False
True

Explanation:
Testcase 1:
1 is present twice in subarray starting from 1 (0-based index) and ending at 4.

 

to report an issue on this page.

Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes

All Submissions

My Submissions:

Login to access your submissions.