Created
December 12, 2016 06:27
-
-
Save pungrue26/b6baf599fa0f3abc10af667b2089915c to your computer and use it in GitHub Desktop.
GeeksforGeeks, Largest Number formed from an Array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.lang.*; | |
import java.io.*; | |
class GFG { | |
public static void main (String[] args) { | |
Scanner in = new Scanner (System.in); | |
int t = in.nextInt(); | |
for (int i = 0; i < t; i++) { | |
int n = in.nextInt(); | |
String [] array = new String [n]; | |
for (int j = 0; j < n; j++) { | |
array[j] = String.valueOf(in.nextInt()); | |
} | |
System.out.println(form(array)); | |
} | |
in.close(); | |
} | |
private static String form (String [] array) { | |
Arrays.sort(array, new Comparator<String>(){ | |
@Override | |
public int compare(String s1, String s2) { | |
return comp(s1, s2); | |
} | |
}); | |
StringBuilder sb = new StringBuilder(); | |
for (int i = array.length - 1; i >= 0; i--) { | |
sb.append(array[i]); | |
} | |
return sb.toString(); | |
} | |
private static int comp(String s1, String s2) { | |
int i1 = 0, i2 = 0; | |
while (i1 < s1.length() && i2 < s2.length()) { | |
if (s1.charAt(i1) < s2.charAt(i2)) { | |
return -1; | |
} else if (s1.charAt(i1) > s2.charAt(i2)) { | |
return 1; | |
} | |
i1++; | |
i2++; | |
} | |
if (i1 >= s1.length() && i2 >= s2.length()) { | |
return 0; | |
} else if (i1 >= s1.length()) { | |
return comp(s1, s2.substring(i2)); | |
} else { | |
return comp(s1.substring(i1), s2); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
public class Practice {
}