Given an array of integers, sort the array (in descending order) according to count of set bits in binary representation of array elements

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

bool cmp(int a,int b)
{
int ca=0,cb=0;
while(a)
{
a=a&(a-1);
ca++;
}
while(b)
{
b=b&(b-1);
cb++;
}
return ca>cb;
}

int main() {
//code
int t;
cin>>t;
while(t–)
{
long long int n;
cin>>n;
long long int ar[n];
for(long long int i=0;i<n;i++)
cin>>ar[i];
sort(ar,ar+n,cmp);
for(long long int i=0;i<n;i++)
cout<<ar[i]<<" ";
cout<<endl;
}

return 0;

}

//sir code is not passing all the test cases

@firozbhaikardar21 please share the question link

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.