Largest number with given sum | Practice | GeeksforGeeks

Geek lost the password of his super locker. He remembers the number of digits N as well as the sum S of all the digits of his password. He know that his password is the largest number of N digits that can be made with given sum S. As he is busy doing his homework, help him retrieving his password.
Example 1:
Input:
N = 5, S = 12
Output:
93000
Explanation:
Sum of elements is 12. Largest possible 
5 digit number is 93000 with sum 12.

Example 2:
Input:
N = 3, S = 29
Output:
-1
Explanation:
There is no such three digit number 
whose sum is 29.
Your Task : 
You don't need to read input or print anything. Your task is to complete the function largestNumber() which takes 2 integers N and S as input parameters and returns the password in the form of string, else return "-1" in the form of string.
Constraints:
1 ≤ N ≤ 104
0 ≤ S ≤ 9*104
Expected Time Complexity : O(N)
Expected Auxilliary Space : O(1)
0
shaank181816 hours ago
Java Code
class Solution
{
    //Function to return the largest possible number of n digits
    //with sum equal to given sum.
    static String largestNumber(int n, int sum)
    {
        String ans = "";
        
        if(sum>9*n){
            return "-1";
        }
        
        
        for(int i = 0;i < n ;i++){
            if(sum>=9){
                sum -= 9;
                ans = ans + 9;
            }
            else if(sum<9){
                ans =  ans + sum;
                sum = sum - sum;
            }
        }
        
        return ans;
    }
}

0
princejee20191 month ago
C++ || EASY TO UNDERSTAND 
 string largestNumber(int n, int sum){       string res ="";       int ans =0;       if(sum>(n*9))return "-1";       int d = sum/9;       for(int i =0;i<d;i++){           res+='9';       }       if(sum%9!=0){           res+='0'+sum%9;       }       while(res.size()!=n){           res+='0';       }       return res;   }
0
praveenkumar1391521 month ago
    def largestNum(self,n,s):       k=''       if s>n*9:           return -1       else:           while s>9:               k=k+str(9)               s=s-9           else:               k=k+str(s)       while len(k)!=n:           k=k+"0"       return k
+1
naveenapatnaik1 month ago
Java SOLUTION!!
 
static String largestNumber(int n, int sum)   {       // add your code here       if(sum > n*9){           return "-1";       }       String str="";       for(int i=0; i<n; i++){           if(sum>=9){               str+="9";               sum-=9;           }           else{             str+=sum;             sum=0;           }       }       return str;          }
0
ayushnautiyal11101 month ago
string largestNumber(int n, int sum)   {       if(sum>n*9){           return "-1";       }       else{           string ans="";           while(sum){               if(sum>=9){                   sum-=9;                   ans+="9";               }               else if(sum>=8){                   sum-=8;                   ans+="8";               }               else if(sum>=7){                   sum-=7;                   ans+="7";               }               else if(sum>=6){                   sum-=6;                   ans+="6";               }               else if(sum>=5){                   sum-=5;                   ans+="5";               }               else if(sum>=4){                   sum-=4;                   ans+="4";               }               else if(sum>=3){                   sum-=3;                   ans+="3";               }               else if(sum>=2){                   sum-=2;                   ans+="2";               }               else if(sum>=1){                   sum-=1;                   ans+="1";               }           }           if(ans.length()!=n){               int diff=n-ans.length();               while(diff--){                   ans+="0";               }           }           return ans;       }   }
0
abhinavmaurya2022 months ago
class Solution:   def largestNum(self,n,s):       if(9*n < s):           return "-1"       sumnum = 9*n       i = n - 1       strnum = [9]*n       while(sumnum != s):           sumnum -= 1           strnum[i] = strnum[i] - 1           if (strnum[i] == 0):               i -= 1       strres = ""       for i in strnum:           strres += str(i)       return strres
0
sachinupreti1902 months ago
class Solution{   public:   //Function to return the largest possible number of n digits   //with sum equal to given sum.
// Easy explanation  Please Upvote if it helps....   string largestNumber(int n, int sum)   {       // Your code here       int i;       string str="";       while(n!=0)       {          if(sum<9)          {              str+=to_string(sum);              sum-=sum;          }          else if(sum >=9)          {              str+=to_string(9);              sum-=9;          }          else if (sum==0)          str+="0";          n--;       }       if(sum == 0)       return str;       else       return "-1   }};                                   
+2
siddhant072 months ago
    string largestNumber(int n, int sum)
    {
        string ans = "";
        if(9*n < sum){
            return "-1";
        }
        while(ans.size() < n){
            int dig = std::min(sum , 9);
            sum -= dig;
            ans += to_string(dig);
        }
        return ans;
    }
-1
vibhors352Premium2 months ago
C++
 string largestNumber(int n, int sum)   {       // Your code here       if(sum>9*n) return "-1";       string ans="";       for(int i=0;i<n;i++){           if(sum>9){               ans+="9";               sum-=9;           }           else{               ans+=to_string(sum);               sum=0;           }       }       return ans;   }
+1
kushwahasuraj3792 months ago
 string largestNumber(int n, int sum)   {       int j=9;       string s = "";       bool flage = true;       for(int i=0;i<n;i++)       {           if(j <= sum)           {               sum -=j;               string str= to_string(j);               s +=str;           }           else if(flage)           {               string str= to_string(sum);               s +=str;               sum =0;               flage = false;           }           else            s +="0";       }       if(sum >0)       return "-1";       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.