#include
#include
using namespace std;
int main(){
int T,N,K;
cin>>T;
while(T- - ){
cin>>N>>K; int A[N],max1=0,j;
for(int i=0;i<N;i++){cin>>A[i];}
set S;
for(int i=0;i<1;i++){
j=i;
while(S.size()!=K && j<N){
S.insert(A[j]); j++;
}
if(j==N){j++;}
S.clear();
max1=max(max1,j-i-1);
}
cout<<max1<<endl;
}
return 0;
}
A humble request: From the next time, please format the code properly or provide a link to your submission
The link for your submission is CodeChef: Practical coding for everyone
There are two mistakes in your code.
First
for(int i=0;i<1;i++)
I think you meant for(int i=0;i<N;i++)
Second
if(j==N){j++;}
This won’t work in a case like
1
3 3
1 2 3
You have to make sure the size of the set is less than K
You have to change the if statement to
if(j==N && S.size()!=K){j++;}
Partial AC after modifying your code: CodeChef: Practical coding for everyone
2 Likes
Thanks a lot buddy . Sillly mistakes !!
I will take care about proper formatting and link from next time .
2 Likes