MaxAnd18 June Lunchtime

Can anyone help what is wrong with my code, I am getting WA for all the cases. This was the problem of June LunchTime

void Main(){
int n,k;
cin>> n>> k;
vector v(n,0);
for(int i=0; i<n; i++){
cin>> v[i];
}
vector countBit(30,0);
for(int i=0;i<n;i++){
for(int j=0;j<30;j++){
if(v[i] & (1<<j)){
countBit[j]++;
}
}
}
long long x = 1;
for(int i=0;i<30;i++){
countBit[i] = x*countBit[i];
x += x;
}
long long ans = 0;
int val = -1;
for(int i=0; i<n; i++) {
int counter = 0;
long long tempans = 0;
for(int j=0;j<30;j++){
if(v[i] & (1<<j)){
counter++;
tempans = tempans + countBit[j];
}
}
if(counter!=k) continue;
if(ans<tempans) {
ans = tempans;
val = v[i];
} else if(ans == tempans){
if(val == -1) val = v[i];
val = min(val,v[i]);
}
}
cout<<val<<Endl;
return ;
}

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t; cin>>t;
while(t–)
Main();
return 0;
}

No need to find it. I got that. I was choosing X from the array itself. :sweat_smile: