Find smallest number with given number of digits and sum of digits
#include<stdio.h>
int main() {
int sum,d;
scanf("%d%d",&sum,&d);
if(sum == 0)
{
if(d == 1)
printf("0");
else
printf("Not Possible");
}
else if(sum > 9*d)
printf("Not Possible");
else
{
int ans[d],i;
sum = sum-1;
for(i=d-1;i>0;i--)
{
if(sum>9)
{
ans[i]=9;
sum = sum -9;
}
else
{
ans[i] = sum;
sum=0;
}
}
ans[0]=sum+1;
for(i=0;i<d;i++)
printf("%d",ans[i]);
}
}
No comments:
Post a Comment