Help me in solving CNDY problem

My issue

I have written this code. I think my calculation & logic is right, but it's not calculating proper. Please, help me someone to find, what is the problem in this.

My code

#include <iostream>
using namespace std;

int main() {
	
	int t; cin>>t; 
	while(t--)
	{
	    int N; cin>>N;
	    int A[2*N];
	    for(int i=0; i<2*N; i++)
	    {
	        cin>>A[i];
	    }
	    
	    int flag = 0;
	    for(int k=0; k<(2*N)-1; k++)
	    {
	        for(int i=k; i<2*N; i++)
	        {
	            if(A[k] == A[i]){
	                flag ++;
	                if(flag > 2) break;
	            } 
	        }
	        if(flag > 2) 
	        {
	            cout << "No" <<endl;
	            break;
	        }
	    }
	    
	    if(flag < 3)
	    {
	        cout << "Yes"<< endl;
	    }
	}
	return 0;
}

Problem Link: CNDY Problem - CodeChef

@sougata20702
include
using namespace std;

int main() {

int t; cin>>t; 
while(t--)
{
    int N; cin>>N;
    int A[2*N];
    for(int i=0; i<2*N; i++)
    {
        cin>>A[i];
    }
    
    int flag = 0;
    for(int k=0; k<2*N; k++)
    {
        flag=0;
        for(int i=0; i<2*N; i++)
        {
            if(A[k] == A[i]){
                flag ++;
                if(flag > 2) break;
            } 
        }
        if(flag > 2) 
        {
            cout << "No" <<endl;
            break;
        }
    }
    
    if(flag < 3)
    {
        cout << "Yes"<< endl;
    }
}
return 0;

}
I have corrected your code . U were making a little mistake in the nested loop part . u will get it after seeing this .

@sougata20702 here we meet again pal!!
the approach is pretty simple , if any element has frequency greater than 2 this means it will get repeated in any of the 2 arrays which will result the output to be negative.

i have pasted my correct code below

hope this helps !

include <bits/stdc++.h>
using namespace std;
int main() {

int t;
cin>>t;
while(t--)
{  
    int n;
    cin>>n;
    int c=0;
   unordered_map<int,int>mp;
    for(int i=0;i<2*n;i++)
    {
        int a;
        cin>>a;
        mp[a]++;
    }
    for(auto it: mp)
    {
        if(it.second>2)
        {
           c++;
        }
    }
 if(c>0)
 {
     cout<<"no"<<endl;
 }
 else 
 cout<<"yes"<<endl;
}
return 0;

}