Knowee
Questions
Features
Study Tools

Given two integers s and d. The task is to find the smallest number such that the sum of its digits is s and the number of digits in the number are d. Return a string that is the smallest possible number. If it is not possible then return -1.

Question

Given two integers s and d. The task is to find the smallest number such that the sum of its digits is s and the number of digits in the number are d. Return a string that is the smallest possible number. If it is not possible then return -1.

🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a Python solution for the problem:

def smallest_number(s, d):
    # If sum of digits is 0, then a number is possible
    # only if number of digits is 1. 
    if s == 0:
        if d == 1:
            return "0"
        else:
            return "-1"
 
    # If sum of digits is more than the maximum
    # possible sum 
    if s > 9*d:
        return "-1"
 
    # Create an array to store digits of result
    res = [0]*d
 
    # Fill the first (d-1) digits (from right to left)
    s -= 1
    for i in range(d-1,0,-1):
        # If sum becomes less than 9, then
        # fill the remaining digits by sum
        if s > 9:
            res[i] = 9
            s -= 9
        else:
            res[i] = s
            s = 0
 
    # Fill the last digit with remaining sum
    res[0] = s + 1  # Earlier subtracted 1 to set first digit as 1
 
    # Convert list to string
    smallest_num = ''.join(map(str,res))
 
    return smallest_num

This function works by first checking if the sum of the digits s is 0, in which case it returns "0" if the number of digits d is 1, and "-1" otherwise. Then it checks if s is greater than the maximum possible sum of d digits, returning "-1" if true.

Next, it creates an array res to store the digits of the result. It fills the first d-1 digits (from right to left) with the maximum possible digit (9) until the sum s becomes less than 9, at which point it fills the remaining digits with the remaining sum.

Finally, it fills the last digit with the remaining sum (plus 1, since we subtracted 1 at the beginning to set the first digit as 1), and converts the list of digits to a string to return as the result.

This problem has been solved

Similar Questions

The smallest two-digit number that is not the sum of three different one-digit numbers isA. 10B. 15C. 23D. 25E. 28

The smallest two-digit number that is not the sum of three different one-digit numbers is

X is a 5 digit number. Let Y be the sum of the digits ofX. Let Z be the sum of the digits of Y. Then themaximum possible value of that Z can have is

A natural number has only two prime factors. It is 100 times the number of its factors. Find the sum of the digits of the least number satisfying these conditions.1432

Emma needs your help in deciding which of the two numbers is smaller. Create a program that takes two integers as input, identifies the minimum using a relational operator, and displays it.Input format :The input consists of two space-separated integers.Output format :The output prints the smallest of the given input numbers.Refer to the sample output for the formatting specifications.Code constraints :In the given scenario, the test cases will fall under the following constraints:-107 ≤ input integers ≤ 107

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.