Wrong answer on 3 test case

#include
using namespace std;
int main() {
int count1 = 0, count2 = 0;
int first, second;

int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++){
    cin>>arr[i];
}

// 2 2 3 1 3 2 1 1 
for(int i=0;i<n;i++){
   if(first==arr[i]){
       count1++;
   }else if(second==arr[i]){
       count2++;
   }else if(count1==0){
       first = arr[i];
       count1=1;
   }else if(count2==0){
       second=arr[i];
       count2=1;
   }else{
       count1--;
       count2--;
   }
}

count1=0,count2=0;
int flag=0;
for(int i=0;i<n;i++){
    if(arr[i]==first){
        count1++;
    }else if(arr[i]==second){
        count2++;
    }
}

    if(count1>n/3){
        flag=1;
        cout<<first<<" ";
    }
    if(count2>n/3){
        flag=1;
        cout<<second<<" ";
    }

    if(flag==0){
        cout<<"No Majority Element";
    }

}

hello @poojas1607
pls save ur code at cb ide and share its url with me

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.