#include<bits/stdc++.h>
using namespace std; #define endl “\n” #define start ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #define test ll t; cin>>t; while(t–) #define M 1000000007
typedef long long int ll;
int main()
{ #ifndef ONLINE_JUDGE
freopen(“input.txt”, “r”, stdin);
freopen(“output.txt”, “w”, stdout); #endif
start
test
{
ll n, k, count=0, ans=0;
cin>> n >> k;
set<ll> check;
for(ll i=0; i<n; i++)
{
ll x;
cin>>x;
// to insert unique elements to count their number
check.insert(x);
count++;
// to check if no. of unique elements are greater than k-1
if( check.size() > (k-1) )
{
if( (count-1) > ans )
ans = (count-1);
count = 1;
check.clear();
check.insert(x);
}
}
if(count > ans)
ans = count;
cout<<ans<<endl;
}
return 0;
}
Is anything wrong with this solution i wanted to insert the elements into set to check no. of unique elements and check if they are greater than (k-1). If they are i cleared the set and inserted the current element into set
for (int i = 1; i <= n; i++) {
ans = max(ans, i - lastoccur[given[i]] - 1);
lastoccur[given[i]] = i;
}
for (int i = 1; i <= k; i++)
ans = max(ans, n - lastoccur[i]);
printf("%d\n", ans);
}
return 0;
t = int(input())
for _ in range(t):
n,k = input().split()
n = int(n)
k = int(k)
main= list(map(int,input().split()))
i = 0
ans = 0
while(i<n):
j = i
temp = []
count = 0
maxc = k-1
while(j<n and maxc>=0):
if main[j] not in temp:
maxc-=1
if(maxc<0):
break
temp.append(main[j])
if(len(temp)==2):
i = j
count +=1
else:
count+=1
j+=1
if(len(temp)<=1):
i = n
if(count>ans):
ans=count
print(ans)
Someone help me understand why my code fails, it’s partially correct for all except task 0 of the first subtask (WA). I follow pretty much the editorial’s solution in my code
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define compSpeed ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
int32_t main(){
compSpeed;
int t;
cin>>t;
while(t--){
int n,k;
cin>>n>>k;
int arr[n];
for(int i=0;i<n;i++)
cin>>arr[i];
int l=0,r=1;
int cake[n+1] = {0};
cake[arr[0]] = 1;
int count=1;
int ans=0;
while(1){
while(r<n && count<k){
cake[arr[r]]++;
if(cake[arr[r]] == 1)
count++;
if(count<k) ans=max(ans,r-l+1);
r++;
}
if(r==n)
break;
while(l<=r && count>=k){
cake[arr[l]]--;
if(cake[arr[l]] == 0)
count--;
l++;
}
}
cout<<ans<<endl;
}
return 0;
}
What is wrong with code 50% test cases passed while WA is coming in subtask 2. Please help anyone
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.util.LinkedHashSet;
import java.util.Scanner;
import java.util.Set;
class Codechef {
public static void main(String[] args) throws java.lang.Exception {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int k = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
int x = sc.nextInt();
arr[i] = x;
}
Set<Integer> set = new LinkedHashSet<>();
int l1 = 0, l2 = 0;
int ans = 0;
for (l2 = 0; l2 < n; l2++) {
if (set.size() < k) {
set.add(arr[l2]);
}
if (set.size() == k) {
ans = Math.max(ans, l2 - l1);
set.remove(arr[l1]);
if (set.size() == 1) {
l1 = l2;
} else {
++l1;
}
}
}
ans = Math.max(ans, l2 - l1);
bw.write(ans + "\n");
}
sc.close();
bw.close();
}