jayati

Largest number possible

Oct 26th, 2023
874
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.96 KB | None | 0 0
  1. //{ Driver Code Starts
  2. // Initial Template for C++
  3.  
  4. #include <bits/stdc++.h>
  5. using namespace std;
  6.  
  7. // } Driver Code Ends
  8. // User function Template for C++
  9.  
  10. class Solution{
  11. public:
  12.     string findLargest(int N, int S){
  13.         // code here
  14.         string s="";
  15.         int sum=0;
  16.        
  17.        
  18.         if(S==0 && N>1)
  19.         {
  20.             return "-1";
  21.         }
  22.          for(int i=0;i<N;i++)
  23.         {
  24.            if(S>=9)
  25.            {
  26.                s+='9';
  27.                S-=9;
  28.            }
  29.            else
  30.            {
  31.                s+=char(S+48);
  32.                S=0;
  33.            }
  34.         }
  35.         if(S>0)
  36.         {
  37.             return "-1";
  38.         }
  39.         return s;
  40.        
  41.        
  42.     }
  43. };
  44.  
  45. //{ Driver Code Starts.
  46.  
  47. int main(){
  48.     int t;
  49.     cin>>t;
  50.     while(t--){
  51.         int N, S;
  52.         cin>>N>>S;
  53.        
  54.         Solution ob;
  55.         cout<<ob.findLargest(N, S)<<"\n";
  56.     }
  57.     return 0;
  58. }
  59. // } Driver Code Ends
Advertisement
Add Comment
Please, Sign In to add comment