#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;cin>>t;
while(t–)
{
map<int,int>mp,p;
sets;
int n,k,a=0,f=1;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>k;
s.insert(k);
if(a!=k&&mp[k]){cout<<“NO”<<endl;f=0;break;}
mp[k]++;
a=k;
}
if(f!=0){
for(auto i:s)
{
if(p[mp[i]]){cout<<“NO”<<endl;f=0;break;}
p[mp[i]]=1;
}
}
if(f)cout<<“YES”<<endl;
}
}
why my code given WA ???
problem link: CHEFRECP Problem - CodeChef
You might stop reading the input for a test case before you read all of it, which will mess your logic up and give you any sort of verdict (probably WA) since you could read some arbitrary value into something like n.
thanks
Why is my code giving WA?
‘’’
#include
#include<unordered_map>
#include<unordered_set>
using namespace std;
int main()
{
int t;
cin>>t;
while(t–)
{
int n;
cin>>n;
int arr[n];
unordered_map<int,int>map;
unordered_setset;
for(int i=0;i<n;i++)
cin>>arr[i];
map[arr[0]]=1;
int t=arr[0],k=0;
for(int i=1;i<n;i++)
{
if(arr[i]==t)
{
map[arr[i]]=1+map[arr[i]];//for frequecy
}
else //will run if a new element comes
{
if(set.find(map[t])==set.end())
set.insert(map[t]);
else
{
k=1;
break;
}
if(map.find(arr[i])==map.end())
{
t=arr[i];
map[arr[i]]=1;
}
else
{
k=1;
break;
}
}
}
if(k==0)
cout<<“YES”<<endl;
else
cout<<“NO”<<endl;
}
return 0;
} ‘’’
Could you scroll up and read the first comment I sent