geeksforgeeks@ largest number formed from an Array

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.