Help me in solving CNDY problem

My issue

there is some mistake in my code where cases are not working can u help me out fixing this question

My code

#include <iostream>

using namespace std;

bool check(int arr[], int n)
{
    for (int i = 0; i < 2 * n; i++)
    {
        int count = 0;
        for (int j = 0; j < 2 * n; j++)
        {
            if (i != j && arr[i] == arr[j])
            {
                count++;
            }
        }
        if (count >= 3)
        {
            return false;
        }
    }
    return true;
}

int main()
{
    // your code goes here
    int t;
    cin >> t;
    int n;
    for (int i = 0; i < t; i++)
    {
        cin >> n;
        int *a = new int[n];
        for (int j = 0; j < n; j++)
        {
            cin >> a[j];
        }

        if (check(a, n))
        {
            cout << "YES" << endl;
        }
        else
        {
            cout << "NO" << endl;
        }
    }
    return 0;
}

Problem Link: Candies Practice Coding Problem - CodeChef

@adithya2152
plzz refer my c++ code for better understanding

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

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        int size = 2 * n;
        int a[size];
        for (int i = 0; i < size; i++)
        {
            cin >> a[i];
        }

        sort(a,a+size);
        string s = "YES";
        
        for (int i = 0; i < size-2; i++)
        {
            if(a[i]==a[i+1] && a[i+1]==a[i+2]){
                s="NO";
            }
        }
        cout<<s<<"\n";
        
    }
    return 0;
}