Find maximum number | Practice | GeeksforGeeks

Given a number N, write a program to find a maximum number that can be formed using all of the digits of this number.
Note: The given number can be very large, so the input is taken as a String.
 
Example 1:
Input:
N = "38293367"
Output:
98763332 
Explanation:
98763332 is the largest number that
can be formed by rearranging the
digits of N.
Example 2:
Input:
N = "1203465"
Output:
6543210
Explanation:
6543210 is the largest number that
can be formed by rearranging the
digits of N.
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function findMax() which takes a String N as input and returns the answer.
 
Expected Time Complexity: O(|N|)
Expected Auxiliary Space: O(constant)
 
Constraints:
1 <= |N| <= 105
+1
imohdalam2 months ago
JAVA | O(n)
class Solution {
    static String findMax(String N) {
        
        int n = N.length();
        int[] arr = new int[n];
        StringBuilder ans = new StringBuilder();
        
        for(int i=0; i<n; i++){
            char ch = N.charAt(i);
            int temp = ch - '0';
            arr[i] = temp;
        }
        
        Arrays.sort(arr);
        
        for(int i=n-1; i>=0; i--)
            ans.append(arr[i]);
        
        return ans + "";
    }
};
0
asifsaba5812 months ago
 string Reverse(string n){
     
     
     int startIdx=0;
     
     int endIdx=n.size()-1;
     
     while(startIdx<=endIdx){
         
         int temp=n[startIdx];
         
         n[startIdx]=n[endIdx];
         
         n[endIdx]=temp;
         
         startIdx++;
         
         endIdx--;
     }
     
     return n;
     
 }
 
    string findMax(string N) {
     
     sort(N.begin(),N.end());
        
        return Reverse(N);
    }
+1
technophyle13 months ago
Easy Code with Expected Time Complexity and Space Complexity :)
string findMax(string N)
 {
        // code here
        string res="";
        vector<int> v(10,0);
        for(char x:N)
        	v[x-'0']++;
        for(int i=9;i>=0;i--)
        {
            while(v[i]>0)
            {
            	res+=to_string(i);
            	v[i]--;
            }
        }
        return res;
  }
One Line Code :)
string findMax(string N)
 {
        // code here
        sort(N.begin(),N.end(),greater<char>());
        return N;
 }
0
19bcs1187
This comment was deleted.
0
tyagis46063 months ago
C++ solution 
string findMax(string N) {       // code here       sort(N.begin(),N.end());       reverse(N.begin(),N.end());       return N;   }
+2
shubhamkhavare3 months ago
Simple Java Solution:
char[] c1 = N.toCharArray();       Arrays.sort(c1);       String s = new String();       for(int i = c1.length-1 ; i >= 0 ; i--)       {           s += c1[i];       }       return s;
0
abhishekguptaaa4 months ago
satisfying the expected TC and SC:
class Solution { public:   string findMax(string N) {                     int arr[10]={0};              for(int i=0;i<N.length();i++)       {           arr[N[i]-'0']++;       }              string res;              for(int i=9;i>=0;i--)       {           if(arr[i]>0)           {               while(arr[i]>0)               {                   res+=to_string(i);                   arr[i]--;               }           }       }                     return res;          }};
0
krithikanithyanandam14 months ago
#Python3 one line solution
def findMax(self, N):
        return (''.join(sorted(N)))[::-1]
0
amiransarimy4 months ago
Python One Line Solutions
 
class Solution:
   def findMax(self, N):
       return ''.join(reversed(sorted(N)))
0
gauravpoharkar634 months ago
Java Solution:
 
class Solution {   static String findMax(String N) {       // code here       String s="";       char [] arr=N.toCharArray();       Arrays.sort(arr);      for(int i =(arr.length-1);i>=0;i--)        s+=arr[i];      return s;   }};
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.