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

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        if(n==1)
        cout<<"YES"<<"\n";
        else
        {
            vector<int>a;
            for(int i=0;i<n;i++)
            {
                int  c;
                cin>>c;
                a.push_back(c);
            }
            
            vector<int>::iterator it; 
    
            int flag=0;
            for(int i=0;i<n-1;i++)
            {
                if(a[i]==a[i+1])
                continue;
                else
                {
                    it = find(a.begin()+i+1,a.end(),a[i]);
                    if(it!=a.end())
                    {
                        //cout<<"reason 1";
                        flag =1;
                        break;
                    }
                }
            }
            if(flag ==1)
            {
                cout<<"NO"<<"\n";
            }
            else
            {
                int f = 0;
                int hash[1001];
                for(int i=1;i<1001;i++)
                {
                    hash[i]=0;
                }
                for(int i=0;i<n;i++)
                {
                    hash[a[i]]++;
                }
                // cout<<"hash" ;
                // for(int i=1;i<1001;i++)
                // cout<<hash[i]<<" ";
                 int cou = hash[a[0]];
                //  cout<<cou<<" ";
               
                for(int i=0;i<n-1;i++)
                {
                    if(a[i] == a[i+1]) 
                    continue;
                    else
                    {
                        if(cou == hash[a[i+1]])
                        {
                           // cout<<"reason 2";
                            f=1;
                            break;
                        }
                        else 
                        cou = hash[a[i+1]];
                    }
                }
                if(f ==1)
                cout<<"NO"<<"\n";
                else
                cout<<"YES"<<"\n";
            }
        }
    }
    return 0;
}

Generated URL:

Output:

NO
NO
NO
YES
NO
YES
NO
NO
Report Bug