Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.lang.*;
- import java.io.*;
- public class Main
- {
- public static int count(int n)
- {
- int count = 0;
- while(n>0)
- {
- n = n & (n-1);
- count++;
- }
- return count;
- }
- public static void main (String[] args) throws java.lang.Exception
- {
- //your code here
- Scanner s = new Scanner(System.in);
- int t = s.nextInt();
- for(int x=1;x<=t;x++)
- {
- int n = s.nextInt();
- int[] arr = new int[n];
- for(int i=0;i<n;i++)
- {
- arr[i] = s.nextInt();
- }
- int k = 0;
- for(int i=0;i<n;i++)
- {
- int max = count(arr[i]);
- for(int j=i+1;j<n;j++)
- {
- if(count(arr[j])>max)
- {
- max = count(arr[j]);
- int temp1=arr[i];
- arr[i]=arr[j];
- arr[j]=temp1;
- }
- else if(count(arr[j])==max && arr[i]>arr[j]){
- int temp1=arr[i];
- arr[i]=arr[j];
- arr[j]=temp1;
- }
- }
- }
- for(int i =0;i<n;i++)
- {
- System.out.print(arr[i]+" ");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement