fork(2) download
  1. /* package codechef; // 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 Codechef
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. String str = br.readLine();
  13. int intTestCase = Integer.parseInt(str);
  14. for(int i = 0 ; i < intTestCase;i++ ){
  15. String lengthOfStr = br.readLine();
  16. boolean value = false;
  17. String intputString = br.readLine();
  18. StringBuilder buff = new StringBuilder(intputString);
  19. int reverse = 0;
  20. for(int j = intputString.length() - 1 ; j >= 2; j=j-2){
  21. int tmp1 = intputString.charAt(j) - 48;
  22. int tmp2 = intputString.charAt(j-2) - 48;
  23. if(tmp1 > tmp2){
  24. buff.setCharAt(j,(char) (tmp2 + 48));
  25. buff.setCharAt(j-2,(char) (tmp1 + 48));
  26. value = true;
  27. reverse = j;
  28. break;
  29. }
  30. }
  31. if(value){
  32. int minValue = 0;
  33. int indexToSwap = -1;
  34. for(int k=reverse;k <= intputString.length() - 2;k+=2){
  35. minValue = buff.charAt(k) - 48;
  36. for(int h = k +2; h <= intputString.length() - 1; h+=2){
  37. int tmp1 = buff.charAt(h) - 48;
  38. if(minValue > tmp1){
  39. minValue = tmp1;
  40. indexToSwap=h;
  41. }
  42. }
  43. if(indexToSwap != -1){
  44. buff.setCharAt(indexToSwap,(char) (buff.charAt(k)));
  45. buff.setCharAt(k, (char) (minValue+48));
  46. }
  47. indexToSwap = -1;
  48. }
  49. }
  50. if(value){
  51. String result = new String(buff);
  52. System.out.println(result.replaceAll(" ",""));
  53.  
  54. }
  55. else{
  56. int l = -1;
  57. System.out.println(l);
  58. }
  59. }
  60. }
  61. }
Success #stdin #stdout 0.06s 33496KB
stdin
2
1
1
2
1 3
stdout
-1
31