fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. static void separate4Colors(int[] a) {
  11. int[] index = new int[4];
  12. for(int n : a) {
  13. int r = n % 4;
  14. if (r != 3) {
  15. index[r+1]++;
  16. }
  17. }
  18. index[2] += index[1];
  19. index[3] += index[2];
  20. int[] res = new int[a.length];
  21. for(int n : a) {
  22. res[index[n%4]++] = n;
  23. }
  24. System.arraycopy(res, 0, a, 0, a.length);
  25. }
  26. public static void main (String[] args) throws java.lang.Exception
  27. {
  28. int[] a = new int[]{0,2,4,5,6,8,7,9,10,12,14,15,17,20,1};
  29. System.out.println(Arrays.toString(a));
  30. separate4Colors(a);
  31. System.out.println(Arrays.toString(a));
  32. }
  33. }
Success #stdin #stdout 0.09s 320320KB
stdin
Standard input is empty
stdout
[0, 2, 4, 5, 6, 8, 7, 9, 10, 12, 14, 15, 17, 20, 1]
[0, 4, 8, 12, 20, 5, 9, 17, 1, 2, 6, 10, 14, 7, 15]