-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eb43d72
commit 1666ffb
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
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
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; | ||
} | ||
} |