CHEFRECP - Editorial

I have a doubt here if I use A : {1,1, 4, 2, 2, 2} freq : {0, 2, 3, 0, 1, 0} and times : {3, 1, 1, 1, 0, 0}
then on check it times[i] > 1 flag will be false then answer will get “NO” but it should be “YES”. Please tell me where I am wrong?

bro can u explain what does this do
int m[10];
int a[10];
m[a[i]]++;
what does m[a[i]]++ do and how it works

#include
#include<bits/stdc++.h>
using namespace std;
int main()
{
cout<<“enter test cases”<<endl;
int t;
int repeat=0;
cin>>t;

int n;
int i;
int a[10];
while(t>0)
{
cout<<“enter size of array”<<endl;
cin>>n;
cout<<“enter elements of array”<<endl;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
for(int i=0;i<n;i++)
{
for(int j=i+1;j<=n;j++)
{
if(a[i]==a[j])
{
repeat++;
}
}
}
if(repeat%2==0)
{
cout<<“yes”<<endl;
}
else
{
cout<<“no”<<endl;
}
t–;
}
}

may be it is

for i in range(n):

m[a[i]] += 1

It is used to count the frequency of integers in a.

what is wrong in my code someone please help it gives wrong answer

CODE:
#include <bits/stdc++.h>
#include
#include
using namespace std;

int main()
{
int t,n;
cin>>t;
for(int i=0;i<t;i++)
{
cin>>n;
int a[n];
for(int i=0;i<n;i++)
{
cin>>a[i];
}
int x=1;
int z=0;
int b[n]={0};
for(int i=0;i<n;i++)
{
if(a[i]==a[i+1])
{
x++;
}
else{
b[z]=x;
z++;
x=1;
}
}

    sort(b,b+z);
    int c=0;
    for(int i=0;i<z;i++)
    {
        if(b[i]==b[i+1])
        {
            c++;
        }
    }
    if(c==0)
    {
        cout<<"YES"<<"\n";
    }
    else{
        cout<<"NO"<<"\n";
    }
    
}
return 0;

}

  1. map ‘m’ stores count of each element, and also out “NO” when, previous index element is not same when, that element’s count is greater than 0.

  2. ‘duplicateCheck’ is a set, means if same number of count occurs. Set will not add it. And length will be not same as map ‘m’.

  3. It has passed given test case. Why it is showing error “W.A.” I have tried every possible test cases? Help?

     void solve() {
         int n;
         cin >> n;
         int r[n];
         map<int, int> m;
         set<int> duplicateCheck;
    
         for (int i = 0; i < n; i++) {
           cin >> r[i];
           if (m[r[i]] > 0) {
             if (r[i - 1] != r[i]) {
               cout << "NO" << endl;
               return;
             } else {
               m[r[i]]++;
             }
           } else {
             m[r[i]] = 1;
           }
         }
    
         for (auto i = m.begin(); i != m.end(); i++) {
           duplicateCheck.insert((*i).second);
         }
    
         duplicateCheck.size() == m.size() ? cout << "YES" << endl : cout << "NO" << endl;
     }

My time limit is getting exceeded.Please check how can i make it faster.

#include<bits/stdc++.h>
using namespace std;
int main(){
    int t,n;
    cin>>t;
    while(t--){
        cin>>n;
        int a[n],max=0;
        for(int i=0;i<n;i++){
            cin>>a[i];
            if(a[i]>max){
                max=a[i];
            }
        }
        int freq[max+1],times[max+1];
        for(int i=0;i<max+1;i++){
            freq[i]=0;
            times[i]=0;
        }
        for(int i=0;i<n;i++){
            freq[a[i]]++;
        }
        int cnt=0;
        for(int i=0;i<max+1;i++){
            if(freq[i]>0){
                cnt++;
            }
        }
        for(int i=0;i<max+1;i++){
            if(freq[i]!=0){
            times[freq[i]]++;
            }
            
        }
        bool flag=true;
        for(int i=0;i<max+1;i++){
            if(times[i]>1){
                flag=false;
            }
        }
        int val=1;
        for(int i=1;i<n;i++){
            if(a[i]!=a[i-1]){
                val++;
            }
        }
        if(val!=cnt){
            flag=false;
        }
        if(flag){
            cout<<"YES"<<endl;
        }
        else{
            cout<<"NO"<<endl;
        }
        
    }
    return 0;
}

This is the program written in python for CHEFRECP
my python IDE gives proper answer on input.
I need to understand why is it wrong

https://www.codechef.com/viewsolution/33307177

cook your dish here

a=int(input())
l5=[]
for i in range (0,a):
b=int(input())
s1=input()
l1=[]
l1=s1.split(’ ')
l2=[]
pp=0

for k in range (0,len(l1)):
        if(l1[k] not in l2):
            l2.append(l1[k])
            pp=pp+1
        elif(l1[k]==l2[-1]):
            flg="YES"
        else:
            flg="NO"
            break
if(flg=="YES"):
    
    l3=[]
    count=0
    for a in l2:
        count=0
        for j in l1:
            if(a==j):
                count=count+1
        l3.append(count)
    
    j=1
    for ww in l3:
        if (ww in l3[j:]):
            flg="NO"
            break
        j=j+1
if(flg=="YES"):
    l5.append("YES")
    
else:
    l5.append("NO")

print(*l5,sep=’\n’)

Can anyone please help me figure out, why this doesn’t work?
https://www.codechef.com/viewsolution/33290974

Your logic is right. The problem is while taking inputs in r array.
If a case breaks in between taking inputs then in new case these values are taken which gives WA in your case.
You should first store array. Then do operation in map in new loop.

For example,

3
6
1 1 4 2 2 2
8
1 1 4 3 4 7 7 7
8
1 7 7 3 3 4 4 4

In second case value break at second 4.
In third case
Now your new value of n is 7.

someone please tell why I am getting SIGSEGV for this. this was my first cookoff

#include <bits/stdc++.h>
using namespace std;

int main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int t,n;
cin>>t;
while(t–){
cin>>n;
vector v(n);
vector< pair <int,int> > q(n);
bool visited[n];

   for(int i=0;i<n;i++)
   {
   	cin>>v[i];
   	q[i].first=v[i];
   	q[i].second=i;
   	visited[v[i]]=0;
   }
   
   // condition 1 -> frequency of every integer in it is distinct//
   
   unordered_map<int, int> mp; 


   for (int i = 0; i < n; i++) 
    mp[v[i]]++; 
    
   int z=mp.size();
   
   vector<int > freq(z);
   
   int i=0;
   for(auto x : mp){
   	 freq[i]=x.second;
   	 i++;
   }
   
   
   sort(freq.begin(),freq.end());
   
   
   int flag=0;
   for (int i = 1; i <z; i++) {
   	  if(freq[i]==freq[i-1])
   	  {
   	  	
   	  	flag=1;
   	  	break;
		}
    }   
   if(flag==1){
   	cout<<"NO\n";
   	continue;
   }
   
   
   // Condition 2 ->  multiple occurrences occur only as a single contiguous subsequence//
   
   int flag2=0;
   visited[v[0]]=1;
   for(int i=1; i<n; i++){
	if(visited[v[i]]==0)
	{   
	    
		while(v[i]==v[i-1])
		{
			
			visited[v[i]]=1;
			i++;
		}
		visited[v[i]]=1;
		continue;
	}
	else if(visited[v[i]]==1)
	{
		flag2=1;
		break;
	}
   
}
if(flag2==1)
{
	cout<<"NO\n";
	continue;
}

   
cout<<"YES\n";  
   
}
return 0;

}

@quietexplosion Consider the below input

1
3
1000 500 500

in this case, size of your visited array is 3, but you are acessing visited[1000] and visited[500] in the your code.

I tested almost every case but still it’s showing WA, please tell me a failing testcase

    N=int(input().strip())
    for q in range(N):
        n=int(input())
        jars=list(map(int,input().strip().split()))
        d={}
        index=0
        for i in jars:
            if i in d:
                d[i].append(index)
                index+=1
            else:
                d[i]=[index,]
                index+=1
        countArr=[]
        flag=False
        for i in d:
          if len(d[i]) in countArr:
              flag=True
              break
          else:
              countArr.append(len(d[i]))
        if flag :
            print('No')
        else:
            
            for i in d:
              
              for j in range(len(d[i])-1):
                if d[i][j+1]-d[i][j]>1:
                  flag=True
                  break
              if flag:
                break
            if flag:
              print('No')
            else:
              print('Yes')

Can you share link to your submission?

https://www.codechef.com/viewsolution/33330522

Did you get it?

Your code is fine but you are printing Yes instead of YES and same for NO.

Oh no…
Thanks…

Was getting WA , help me to find that wrong logic…

vthz.25

for _ in range(int(input())):

n = int(input())
arri = [int(e) for e in input().split()]
current = arri[0]
temp = []
flag = True
temp.append(current)
temp = []
for i in range(1, len(arri)):

    if arri[i] != current:
        current = arri[i]

        if arri[i] in temp:
            flag = False
            break
        temp.append(current)

nflag = False
if flag:
    dic = {}
    for z in arri:
        if z not in dic:
            dic[z] = 1
        else:
            dic[z] += 1
    #nflag=len(dic)!=len(set(dic.values()))
    #print(dic)
    dic2={}
    for x,value in dic.items():
        #print(str(x)+" "+str(dic[x]))
        if value not in dic2:
            dic2[value]=1
        else:
            dic2[value]+=1
    #print(dic2)


myset=set(arri)
if len(arri)==len(myset):
    nflag=True
if len(dic)==len(dic2):

    nflag=True

if flag and nflag:
    print("YES")
else:
    print("NO")

https://www.codechef.com/viewsolution/33316760