fork download
  1. import java.util.*;
  2. class TestClass{
  3. public static void main(String args[]) throws Exception{
  4. Scanner scan=new Scanner(System.in);
  5. int n=scan.nextInt();
  6. int k=scan.nextInt();
  7.  
  8. List<Integer>[] weights=(List<Integer>[])new List[k];
  9. for(int i=0;i<k;i++){
  10. weights[i]=new ArrayList<>();
  11. }
  12.  
  13. int array1[]=new int[n];
  14. for(int i=0;i<n;i++){
  15. array1[i]=scan.nextInt();
  16. weights[array1[i]%k].add(array1[i]);
  17. }
  18. for(int i=k-1;i>=0;i--){
  19. Collections.sort(weights[i]);
  20. for(int j=0;j<weights[i].size();j++){
  21. System.out.println(weights[i].get(j));
  22. }
  23. }
  24.  
  25. }
  26. }
Success #stdin #stdout 0.07s 2184192KB
stdin
5 2
1 2 3 4 5
stdout
1
3
5
2
4