Count Of Maximum C++ , Why is this getting WA , works fine with the given inputs. Can anyone find the problem here??

#include<bits/stdc++.h>

using namespace std;

int main () {
int k;
cin>>k;

for(int j=0;j<k;j++){
int n;
cin>>n;
int a[n];

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

int maxCount=0;
int number;
int index=0;

for(int i=0;i<n;i++){

int count=0;

    for(int l=0;l<n;l++){
        if(a[i]==a[l]){

               count++;

                if(count>maxCount){
            maxCount=count;
            index=i;
    }
    if(count==maxCount){
        (a[i]>a[index])?number=a[index]:number=a[i];
    }
        }
    }



}

cout<<number<<" "<<maxCount<<endl;
}

}

TS : 8 1 10

Your Output : 8 1

Correct Ans : 1 1

(In case of ties in maximum frequency, select the smaller element.)

In maxcount==count case when you update your number you also need to change the index to i.

Just add index=i in false case in your ternary operator, you’ll get AC.