forked from kothariji/competitive-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path(CODECHEF)Not_All_Flavours.java
56 lines (52 loc) · 1.23 KB
/
(CODECHEF)Not_All_Flavours.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//https://www.codechef.com/LTIME81B/problems/NOTALLFL
/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0)
{
int n=sc.nextInt();
int k=sc.nextInt();
int a[]=new int[n];
int i;
for(i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
int freq[]=new int[100001];
k=k-1;
int start=0, end=0;
int currentCount=0;
int previousElement=0;
for(i=0;i<n;i++)
{
freq[a[i]]+=1;
if(freq[a[i]]==1)
{
currentCount+=1;
}
while(currentCount>k)
{
freq[a[previousElement]]-=1;
if(freq[a[previousElement]]==0)
currentCount-=1;
previousElement+=1;
}
if(i- previousElement+1>= end- start+1)
{
end=i;
start= previousElement;
}
}
System.out.println(end+1-start);
}
}
}