Can anyone tell what is wrong in this code?

problem code : CNDY

I written a code and output matches with a given output but don’t understand how it shows wrong answer after submitting code.

below is my code :slight_smile:

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

int main() {
	// your code goes here

	int t;
	cin>>t;
	
	while(t--){
	    int n;
	    int max=0;
	    int cnt=1;
	    cin>>n;
        
	    int a[2*n];
        
    for(int i=0;i<2*n;i++){
	        cin>>a[i];
	    }
	    
	sort(a,a+(2*n));
	
	for(int i=0;i<2*n-1;i++){
	    if(a[i]==a[i+1]){
	            cnt++;
	        
	       if(cnt>max){
	          max=cnt;  
	        }
	   }
	        
	 }
	    if(max<=2)
	    cout<<"yes"<<endl;
	    else cout<<"no"<<endl;
 }
	return 0;
}


Testcase:

1
2
1 1 2 2

Expected output:

YES

Your Output:

NO

You are solving a slightly different problem. You check if the array contains at most 1 number twice. But you are meant to check if the array does not contain any number more than twice.

1 Like

oh!!
Thanks bro :smile:
I got it !!