fork download
  1. import java.util.Arrays;
  2. import java.util.stream.Collectors;
  3.  
  4. class Main
  5. {
  6. public static void main( String[] args )
  7. {
  8. runTests();
  9. }
  10.  
  11. public static void runTests()
  12. {
  13. //# Input from original post
  14. assertEquals( largestNumber( new int[]{ 3, 30, 34, 5, 9 } ), "9534330" );
  15. //# Other tests from other comments
  16. assertEquals( largestNumber( new int[]{ 30, 301 } ), "30301" );
  17. assertEquals( largestNumber( new int[]{ 3, 30, 34, 90, 9 } ), "99034330" );
  18. //# Invalid assertion
  19. assertEquals( largestNumber( new int[]{ 1, 2, 3 } ), "123" );
  20. }
  21.  
  22. public static String largestNumber( int[] array )
  23. {
  24. String result = Arrays
  25. .stream( array )
  26. .mapToObj( String::valueOf )
  27. .sorted( Main::sort )
  28. .collect( Collectors.joining() );
  29. System.out.println( result );
  30. return result;
  31. }
  32.  
  33. public static int sort( String a, String b )
  34. {
  35. String t1 = a + b;
  36. String t2 = b + a;
  37. return Integer.compare( 0, Integer.valueOf( t1 ).compareTo( Integer.parseInt( t2 ) ) );
  38. }
  39.  
  40. private static void assertEquals( String actual, String expected )
  41. {
  42. if ( !actual.equals( expected ) )
  43. {
  44. System.out.println( "ERROR: Expected " + expected + " but found " + actual );
  45. }
  46. }
  47.  
  48. }
Success #stdin #stdout 0.14s 37448KB
stdin
Standard input is empty
stdout
9534330
30301
99034330
321
ERROR: Expected 123 but found 321