diff --git a/Form the largest number b/Form the largest number new file mode 100644 index 0000000..bea5039 --- /dev/null +++ b/Form the largest number @@ -0,0 +1,56 @@ +//Form the largest number + +import java.io.*; +import java.util.*; + +public class Main { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int t = sc.nextInt(); + sc.nextLine(); + + while(t-- > 0) { + String input = sc.nextLine(); + String[] numbers = input.split(" "); + int[] arr = new int[numbers.length]; + + for(int i = 0; i < numbers.length; i++) { + arr[i] = Integer.parseInt(numbers[i]); + } + + Solution ob = new Solution(); + String ans = ob.findLargest(arr); + System.out.println(ans); + System.out.println("~"); + } + sc.close(); + } +} + +class Solution { + String findLargest(int[] arr) { + ArrayList numbers = new ArrayList<>(); + + for(int ele : arr) { + numbers.add(Integer.toString(ele)); + } + + Collections.sort(numbers, (s1, s2) -> myCompare(s1, s2) ? -1 : 1); + + if(numbers.get(0).equals("0")) { + return "0"; + } + + StringBuilder res = new StringBuilder(); + + for(String num : numbers) { + res.append(num); + } + + return res.toString(); + } + + static boolean myCompare(String s1, String s2) { + return (s1 + s2).compareTo(s2 + s1) > 0; + } +}