Skip to content

Commit

Permalink
Create Form the largest number
Browse files Browse the repository at this point in the history
  • Loading branch information
dishathakurata authored Dec 11, 2024
1 parent eb43d72 commit 1666ffb
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions Form the largest number
Original file line number Diff line number Diff line change
@@ -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<String> 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;
}
}

0 comments on commit 1666ffb

Please sign in to comment.