CODEDECODE -Editorial

PROBLEM LINK:

Practice
Contest

Author: rocknot
Tester: yogesh_5326
Editorialist:rocknot

DIFFICULTY:

EASY

PREREQUISITES:

Implementation

PROBLEM:

Aditya recently bought a new pc, So he needs to configure his pc so that he can start using this pc for his job. His pc has windows-11 so he created a new Microsoft account to signup on this pc. He setup an random password for his account and saved it on notepad. Aditya never used windows 11 before so he didn’t knew that if Microsoft account is setup on the pc, he needs the password of his account every time he starts his Pc. So he switched of his pc but next day when he tried to access his pc, it was locked and he didn’t remember the password. One thing he knows that is the length of password N and sum of all the digits of his password S. Aditya is busy completing his assignments so he needs your help for find his password. You are given an integer N denoting the number of digits in the password and sum S of all the digits of the password. You need to determine the largest N digit integer having sum S. I no such possible number exist print “-1”.

EXPLANATION:

If sum is less than 10 print sum followed by N-1 "0"s.if sum is greater than print 9 while sum is greater than and decrement 9 from the sum if sum is less than 9 print sum followed by remaining 0s and if after N operations sum is not 0 then print -1.

SOLUTIONS:

Solution
#include <bits/stdc++.h>

using namespace std;

void findLargest(int m, int s){

if (s == 0)
{
    (m == 1)? cout << 0: cout << "-1";
    return ;
}
if (s > 9*m)
{
    cout << "-1";
    return ;
}
int res[m];
for (int i=0; i<m; i++)
{
    if (s >= 9)
    {
        res[i] = 9;
        s -= 9;
    }
    else
    {
        res[i] = s;
        s = 0;
    }
}
for (int i=0; i<m; i++)
    cout << res[i];

}
int main(){
int T;
cin>>T;
while(T–){
int N,S ;
cin>>N>>S;
findLargest(N, S);
cout<<endl;
}
return 0;
}