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

c-plus-plus
count
cpp
maximum
of

#1

#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*;
}

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*==a[l]){

               count++;

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



}

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

}


#2

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.