Advertisement
unknown_0711

Untitled

Oct 2nd, 2022
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. public class Main
  6. {
  7. public static int count(int n)
  8. {
  9. int count = 0;
  10. while(n>0)
  11. {
  12. n = n & (n-1);
  13. count++;
  14. }
  15. return count;
  16. }
  17. public static void main (String[] args) throws java.lang.Exception
  18. {
  19. //your code here
  20. Scanner s = new Scanner(System.in);
  21. int t = s.nextInt();
  22. for(int x=1;x<=t;x++)
  23. {
  24. int n = s.nextInt();
  25. int[] arr = new int[n];
  26. for(int i=0;i<n;i++)
  27. {
  28. arr[i] = s.nextInt();
  29. }
  30.  
  31.  
  32.  
  33. int k = 0;
  34. for(int i=0;i<n;i++)
  35. {
  36. int max = count(arr[i]);
  37. for(int j=i+1;j<n;j++)
  38. {
  39. if(count(arr[j])>max)
  40. {
  41. max = count(arr[j]);
  42. int temp1=arr[i];
  43. arr[i]=arr[j];
  44. arr[j]=temp1;
  45. }
  46. else if(count(arr[j])==max && arr[i]>arr[j]){
  47. int temp1=arr[i];
  48. arr[i]=arr[j];
  49. arr[j]=temp1;
  50. }
  51. }
  52.  
  53. }
  54.  
  55. for(int i =0;i<n;i++)
  56. {
  57. System.out.print(arr[i]+" ");
  58. }
  59. }
  60.  
  61.  
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement