http://www.practice.geeksforgeeks.org/problem-page.php?pid=380
Largest number formed from an Array
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 being very large, hence return the result in the form of a string.
Input:
The first line of input consists number of the the test cases. The description of T test cases is as follows:
The first line of all 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
Example:
Input:
2
5
3 30 34) 5 9
4
54 546 548 60
Output:
9534330
6054854654
ImportJava.util.*;Importjava.lang.*;ImportJava.io.*;classEntry { PublicString str; PublicEntry (String s) {Super(); This. str =s; }}classCmpImplementsComparator<entry> { Public intCompare (Entry E1, Entry E2) {String S1=E1.str; String S2=E2.str; StringBuffer Combo1=NewStringBuffer (); StringBuffer Combo2=NewStringBuffer (); Combo1.append (S1); Combo1.append (S2); Combo2.append (S2); Combo2.append (S1); return(Combo2.tostring ()). CompareTo (Combo1.tostring ()); }}classGFG { Public StaticString func (int[] arr) { intn =arr.length; ArrayList<Entry> ls =NewArraylist<entry> (); for(inti=0; i<n; ++i) {Entry Entry=NewEntry (integer.tostring (arr[i)); Ls.add (entry); } collections.sort (LS,NewCMP ()); StringBuffer SB=NewStringBuffer (); for(Entry entry:ls) {sb.append (ENTRY.STR); } returnsb.tostring (); } Public Static voidMain (string[] args) {Scanner in=NewScanner (system.in); intTimes =In.nextint (); for(inti=0; i<times; ++i) {intn =In.nextint (); int[] arr =New int[n]; for(intj=0; j<n; ++j) {Arr[j]=In.nextint (); } System.out.println (func (arr)); } }}
View Code
[email protected] Largest number formed from an Array