Skip to content

Instantly share code, notes, and snippets.

@jaecktec
Created March 6, 2017 16:25
Show Gist options
  • Save jaecktec/bc80b4673c68528534473a54daff45e2 to your computer and use it in GitHub Desktop.
Save jaecktec/bc80b4673c68528534473a54daff45e2 to your computer and use it in GitHub Desktop.
Creates the largest possible Integer using the given Integers
package testproj.uebung;
import java.util.Arrays;
public class Problem4 {
public static void main(String[] args) throws Exception {
System.out.println(arrageLargest(new Integer[] { 501, 50, 56, 12, 1, 19 }));
}
public static String arrageLargest(Integer[] values) {
StringBuilder result = new StringBuilder();
Arrays.sort(values, ((o1, o2) -> {
return compare(o1, o2, 0);
}));
Arrays.asList(values).forEach(v -> result.append(v));
return result.toString();
}
private static int compare(int o1, int o2, int recusionIndex) {
if (o1 == o2) return 0;
int length = String.valueOf(o1).length() > String.valueOf(o2).length() ? String.valueOf(o1).length() : String.valueOf(o2).length();
if (recusionIndex > length) return 0;
if (String.valueOf(o1).charAt(recusionIndex % String.valueOf(o1).length()) > String.valueOf(o2).charAt(recusionIndex % String.valueOf(o2).length())) {
return -1;
} else if (String.valueOf(o1).charAt(recusionIndex % String.valueOf(o1).length()) < String.valueOf(o2).charAt(recusionIndex % String.valueOf(o2).length())) {
return 1;
} else {
return compare(o1, o2, recusionIndex + 1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment