Largest Number formed from an Array | Practice | GeeksforGeeks

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.

Example 1:
Input: 
N = 5
Arr[] = {3, 30, 34, 5, 9}
Output: 9534330
Explanation: Given numbers are {3, 30, 34,
5, 9}, the arrangement 9534330 gives the
largest value.
Example 2:
Input: 
N = 4
Arr[] = {54, 546, 548, 60}
Output: 6054854654
Explanation: Given numbers are {54, 546,
548, 60}, the arrangement 6054854654 
gives the largest value.

Your Task:  
You don't need to read input or print anything. Your task is to complete the function printLargest() which takes the array of strings arr[] as parameter and returns a string denoting the answer.

Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)

Constraints:
1 ≤ N ≤ 105
0 ≤ Arr[i] ≤ 1018
Sum of all the elements of the array is greater than 0.
+1
prateeklucifer262 days ago
EASIEST AND SHORT JAVA CODE.
class Solution {   String printLargest(String[] arr) {       // code here       Arrays.sort(arr, (s1,s2)->(s2+s1).compareTo(s1+s2));       String res = String.join("", arr);       return res;   }}
0
abhishek0908021 week ago
static bool comp(string &a,string &b)
    {
        if(a+b>b+a)
        return 1; 
        else return 0;
    }
    string printLargest(vector<string> &arr) {
        vector<int>temp;
        string ans; 
        sort(arr.begin(),arr.end(),comp);
        for(auto it:arr)
        ans+=it; 
        return ans;
    }
0
gaganwalhdzq1 week ago
string printLargest(vector<string> &arr) {
	    function<bool(string,string)> sort_fun= [](string a, string b)->bool{
	        return (a+b)>(b+a);
	    };
	    sort(arr.begin(),arr.end(),sort_fun);
	   string ans="";
	   for(int i=0;i<arr.size();i++)
	    ans+=arr[i];
	   return ans; 
	}
	very easy approach
0
kamresh4852 weeks ago
string printLargest(vector<string> &arr) {
	    function<bool(string,string)> sort_fun= [](string a, string b)->bool{
	        return (a+b)>(b+a);
	    };
	    sort(arr.begin(),arr.end(),sort_fun);
	   string ans="";
	   for(int i=0;i<arr.size();i++)
	    ans+=arr[i];
	   return ans; 
	}
+5
prakashish3 weeks ago
Java Solution
 
class Solution {
    String printLargest(String[] arr) {
        // code here
        String str[] = new String[arr.length];
        for(int i=0;i<str.length;i++){
            str[i] = String.valueOf(arr[i]);
        }
        Arrays.sort(str, new comp());
        StringBuilder sb = new StringBuilder();
        for(String s : str){
            sb.append(s);
        }
        return sb.toString();
    }
    class comp implements Comparator<String> {
        public int compare(String a, String b){
            return (b+a).compareTo(a+b);
        }
    }
}
+1
surajgorai20023 weeks ago
class Solution{
public:
static bool cmp(string& a, string& b )
{
    return a+b<b+a;
}
	// The main function that returns the arrangement with the largest value as
	// string.
	// The function accepts a vector of strings
	string printLargest(vector<string> &arr) {
	  string ans ="";
	  sort(arr.begin(),arr.end(),cmp);
	  for(int i=arr.size()-1;i>=0;i--)
	  {
	     ans+=arr[i];
	  }
	  return ans ;// code here
	}
};
+1
vishaljaiswal261020003 weeks ago
C++ solution
string printLargest(vector<string> &arr) {
	    // code here
	    sort(arr.begin(),arr.end(),[](string a,string b){
	        return a+b>b+a;});// this gives maximum to minimum comparison using lambda expression
	        string ans;
	        
	        for(string s:arr){
	            ans+=s;
	        }
	        return ans;
	}
0
mrutyunjaykumarmkd3 weeks ago
//java code in few lines
class Solution {   String printLargest(String[] arr) {       // code h       String s="";       Arrays.sort(arr,(a,b)->(b+a).compareTo(a+b));       for(int i=0;i<arr.length;i++){           s+=arr[i];       }       return s;   }}
+1
sanketsupekar4 weeks ago
Java Solution 7.71 Sec
class Solution {
    
    class sortByChar implements java.util.Comparator<String>
    {
        public int compare(String a, String b)
        {
            String x = a+b; 
            String y = b+a;
            return y.compareTo(x);
        }
    }
   
    String printLargest(String[] arr) 
    {
        Arrays.sort(arr, new sortByChar());
        String res = "";
        for(int i=0;i<arr.length;i++)
        res += arr[i];
        return res;
    }
    
}
0
premkodavath984 weeks ago
/*step-1
convert the arrays in to String
step-2
Sort the arrays and then pass two custom strings to compare
step-3
using string builder 
 simple JAVA CODE*/
            int n=arr.length;
            String[] s=new String[n];
            for(int i=0; i<n; i++)
                 s[i]=String.valueOf(arr[i]);
                  Arrays.sort(s, (a,b)→(b+a).compareTo(a+b));
                     
                    StrungBuilder sb= new StringBuilder();
                            for(String str : s)
                             sb.append(str);
       
                   String result= sb.toString();
                    return result;
 
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested 
                                    against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code.
                                    On submission, your code is tested against multiple test cases consisting of all
                                        possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as
                                        the final solution code.
You can view the solutions submitted by other users from the submission tab.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems does not guarantee the 
                                correctness of code. On submission, your code is tested against multiple test cases
                                    consisting of all possible corner cases and stress constraints.