Company: Paytm, Amazon, MakeMyTrip
Output:
Constraints:
Example:
import java.lang.*;
import java.io.*;
class Solution{
public static void main (String[] args) {
Scanner sc = new Scanner (System.in);
int m=sc.nextInt();
for(int mi=0;mi<m;mi++){
int n=sc.nextInt();
Vector <String> ar= new Vector<>();
for(int i=0;i<n;i++){
ar.add(Integer.toString(sc.nextInt()));
}
Collections.sort(ar,new Comparator<String>(){
public int compare(String X, String Y) {
String XY=X + Y;
String YX=Y + X;
return XY.compareTo(YX) > 0 ? -1:1;
}
});
Iterator it = ar.iterator();
while(it.hasNext())
{System.out.print(it.next());}
System.out.println();
}
}
}
Given a list of non negative integers, arrange them in such a manner that they form the largest number possible.
The result is going to be very large, hence return the result in the form of a string.
Input:
The first line of input consists number of the test cases. The description of T test cases is as follows:
The first line of each test case contains the size of the array, and the second line has the elements of the array.
Output:
In each separate line print the largest number formed by arranging the elements of the array in the form of a string.
Constraints:
1 ≤ T ≤ 70
1 ≤ N ≤ 100
0 ≤ A[i] ≤ 1000
1 ≤ N ≤ 100
0 ≤ A[i] ≤ 1000
Example:
Input:
2
5
3 30 34 5 9
4
54 546 548 60
5
3 30 34 5 9
4
54 546 548 60
Output:
9534330
6054854654
6054854654
CODE:
import java.util.*;import java.lang.*;
import java.io.*;
class Solution{
public static void main (String[] args) {
Scanner sc = new Scanner (System.in);
int m=sc.nextInt();
for(int mi=0;mi<m;mi++){
int n=sc.nextInt();
Vector <String> ar= new Vector<>();
for(int i=0;i<n;i++){
ar.add(Integer.toString(sc.nextInt()));
}
Collections.sort(ar,new Comparator<String>(){
public int compare(String X, String Y) {
String XY=X + Y;
String YX=Y + X;
return XY.compareTo(YX) > 0 ? -1:1;
}
});
Iterator it = ar.iterator();
while(it.hasNext())
{System.out.print(it.next());}
System.out.println();
}
}
}
EXECUTION TIME: 0.21s
Comments
Post a Comment