CHEFRECP - Editorial

Hi, my solution passes the test case but still shows as wrong. Can you help me determine why?

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

Thank you.

Why this solution is giving wa
https://www.codechef.com/viewsolution/33389286

My logic is to create two arrays.
Array-1 consists of all Ingredients once, and Array-2 consists of their occurrences of each ingredient.
Example -
Input => 1,1,1,3,3,5,5,5,5,2 ;
Array 1=>1,3,5,2 ;
Array 2=>3,2,4,1 ;

Now I would sort both arrays using qsort() and then test for repetition in ingredients and quantity each array by comparing with previous element.

If both elements equal , than return NO;
else continue till Size, than return YES.

But i still get wrong answer. So what is going wrong here?
Please help.

why it is showing WA.

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

int main() {
// your code goes here
int t;
cin>>t;
while(t–){
int n;
cin>>n;
int a[n],freq[1001],times[n+1];
bool vis[1001];
for(int i=0;i<1001;i++) {
freq[i]=0;
vis[i]=false;
}

    for(int i=0;i<n+1;i++) {
     	times[i]=0;
      }
      
    for(int i=0;i<n;i++){
        cin>>a[i];
        freq[a[i]]++;
    }
    
    for(int j=1;j<=1000;j++){
        times[freq[j]]++;
    }
    
     bool flag=true;  

    for(int i=1;i<=n;i++){
	   if(times[i]>1) 
         flag=false;
	   }
    
    

    bool fla=true;
   vis[a[0]]=true;

for(int i=1;i<n;i++)
{

if(a[i]==a[i-1]) 
	continue;
if(vis[a[i]]==true){ 
	fla=false;
	
}

vis[a[i]]=true;

}

  if(fla==true && flag==true)
  cout<<"Yes";
   else
  cout<<"No";
 
 
 
 
   cout<<"\n";
   
}
return 0;

}

I am a beginner here. Can anyone please guide me , why my code is failing in submission.
It is working correctly with sample case. I am attaching the solution link below.
https://www.codechef.com/viewsolution/33396431

@prince051 Here a case on which your code is failing.

1
5
1 1 1 1 1

But there are mistakes also…you can read the editorial and write the fresh code again.

@sudhir_2116 Output is case sensitive. You are printing “Yes” and “No” instead of “YES” and “NO” respectively.
@cthombare_1011 can you share the submission link?

Thank you . i don’t know how i forgot

I don’t know your logic , but I just want to suggest you that you don’t have to check for the constraints

Blockquoteint T = scanner.nextInt();
if(T<1 || T>100){
System.out.println(“NO”);

They will always give you testcases which follows the constraint.

Blockquote

@rishup_nitdgp

@rishup_nitdgp
My Logic is to create two arraylists and fill it with ingridients and occurences of ingridients.
Then to sort the arraylist . And check if there is any similar elements in any of the arraylists.
Link to my Code :
https://www.codechef.com/viewsolution/33308079
Kindly tell whats the error in the code or for which test case will my code will give wrong output.
Thanks.

Thanks for the help @rishup_nitdgp . I changed my code as per the editorial. But it it is still failing in submission
https://www.codechef.com/viewsolution/33409793

Why is my code giving WA? It passed all the given test cases

#include<iostream>
#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_set<int>set;
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;
}

Why is my code giving WA?It passed all the given sample cases

#include<iostream>
#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_set<int>set;
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]];
	}
	else 
	{
		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;
}

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

Please, tell me just one test case where my solution fail.

@rishup_nitdgp Can you please help me to figure out why it is giving wrong answer

#include <stdio.h>
#include <math.h>
int cmpfunc (const void * a, const void * b) {
return ( (int)a - (int)b );
}

int main(void) {
int t;
scanf("%d",&t);
while(t–)
{
int n,arr[10000],count[10000]={0},ext[10000];
int i,j,k=0,ans=1,p,q=0,x;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
for(i=0;i<n;i++)
{

        if(arr[i]!=arr[i+1])
        count[k]++;
        if(arr[i]==arr[i+1])
        count[k]++;
        else
        k++;
    
}
qsort(count, k, sizeof(int), cmpfunc);
for(i=0;i<k;i++)
{
    if(count[i]==count[i+1])
    {ans=0;break;}
}

// for(i=0;i<k;i++)
// printf("%d ",count[i]);

ext[0]=arr[0];
for(i=0;i<n;i++)
{
    if(ext[q]==arr[i])
    continue;
    else
    {
       for(p=0;p<q;p++)
       {
           if(arr[i]==ext[p])
           {ans=0;
           break;}
       }
       if(ans!=0)
       {
           q++;
           ext[q]=arr[i];
       }
    }
    
}

// for(i=0;i<=q;i++)
// printf("%d ",ext[i]);

if(ans==0)
printf("NO\n");
else
printf("YES\n");
ans=1;
}
return 0;

}

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

//please tell me what is wrong with my code or testcase that fail

Please can someone point out the mistake in my solution? I am iterating through continuous segments of equal elements and checking the following:

  1. The element has not occurred previously
  2. The count of the continuous segment of equal elements has not occurred previously.

Here’s my solution: CodeChef: Practical coding for everyone

Thanks

I got the problem. I was checking the previous occurrence of the element in an incorrect way. Noob mistake :stuck_out_tongue:

Hi there @rishup_nitdgp can u please help me with my code
it is working with all the test cases in the editorial and those provided in the question still i am getting wrong ans plzz help @rishup_nitdgp

#include<bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
int t;
cin>>t;
bool ans=true;
while(t–)
{
int n;
cin>>n;
int a[n];
vectorv;int i;
for( i=0;i<n;i++)
{
cin>>a[i];
}
v.push_back(a[0]);
for( i=1;i<n;i++)
{
if(a[i]!=a[i-1])
v.push_back(a[i]);
}
int s = v.size();
sort(v.begin(),v.end());
for( i=1;i<s;i++){
if(v[i]==v[i-1]){
ans=false;
break;
}
}
v.clear();
if(ans==true){
map<int, int> m;
for ( i = 0; i < n; i++)
m[a[i]]++;
vector q;
int p = m.size();
for(auto j:m){
q.push_back(j.second);}
sort(q.begin(),q.end());
for(i=1;i<q.size();i++){
if(q[i]==q[i-1]){
ans=false;
break;
}
}
q.clear();
m.clear();
}
if(ans==false)
cout<<“NO\n”;
else
cout<<“YES\n”;
}
return 0;
}