Smallest number with sum of digits as N and divisible by 10^N

Find the smallest number such that the sum of its digits is N and it is divisible by 10^N.

Examples :

Input : N = 5
Output : 500000
500000 is the smallest number divisible
by 10^5 and sum of digits as 5.

Input : N = 20
Output : 29900000000000000000000



Explanation

To make a number divisible by 10^N we need at least N zeros at the end of the number. To make the number smallest, we append exactly N zeros to the end of the number. Now, we need to ensure the sum of the digits is N. For this, we will try to make the length of the number as small as possible to get the answer. Thus we keep on inserting 9 into the number till the sum doesn’t exceed N. If we have any remainder left, then we keep it as the first digit (most significant one) so that the resulting number is minimized.

The approach works well for all subtasks but there are 2 corner cases:

1. The first is that the final number may not fit into the data types present in C++/Java. Since we only need to output the number, we can use strings to store the answer.
2. The only corner case where the answer is 0 is N = 0.
3. There are no cases where the answer doesn’t exist.

C++

// CPP program to find smallest 
// number to find smallest number
// with N as sum of digits and 
// divisible by 10^N.
#include <bits/stdc++.h>
using namespace std;

void digitsNum(int N)
{
    // If N = 0 the string will be 0
    if (N == 0) 
        cout << "0\n";
    
    // If n is not perfectly divisible 
    // by 9 output the remainder 
    if (N % 9 != 0) 
        cout << (N % 9);
    
    // Print 9 N/9 times
    for (int i = 1; i <= (N / 9); ++i) 
        cout << "9";
    
    // Append N zero's to the number so
    // as to make it divisible by 10^N
    for (int i = 1; i <= N; ++i) 
        cout << "0";
    
    cout << "\n";
}

// Driver Code
int main()
{
    int N = 5;
    cout << "The number is : ";
    digitsNum(N);
    return 0;
}

Java

// Java program to find smallest 
// number to find smallest number 
// with N as sum of digits and 
// divisible by 10^N.
import java.io.*;

class GFG 
{

static void digitsNum(int N)
{
    // If N = 0 the string will be 0
    if (N == 0) 
    System.out.println("0");
        
    
    // If n is not perfectly divisible
    // by 9 output the remainder
    if (N % 9 != 0) 
        System.out.print((N % 9));
    
    
    // Print 9 N/9 times 
    for (int i = 1; i <= (N / 9); ++i) 
        System.out.print("9");
        
    
    // Append N zero's to the number so
    // as to make it divisible by 10^N 
    for (int i = 1; i <= N; ++i) 
        System.out.print("0");
        System.out.print("" );
    
}

    // Driver Code
    public static void main (String[] args) 
    {
    int N = 5;
    System.out.print("The number is : ");
    digitsNum(N);
    }
} 

// This code is contributed by vt_m

Python3

# Python program to find smallest 
# number to find smallest number 
# with N as sum of digits and 
# divisible by 10^N.

import math
def digitsNum(N):

    # If N = 0 the string will be 0 
    if (N == 0) :
        print("0", end = "")
    
    # If n is not perfectly divisible
    # by 9 output the remainder 
    if (N % 9 != 0): 
        print (N % 9, end ="")
    
    # Print 9 N/9 times 
    for i in range( 1, int(N / 9) + 1) :
        print("9", end = "")
    
    # Append N zero's to the number so 
    # as to make it divisible by 10^N 
    for i in range(1, N + 1) :
        print("0", end = "")
    
    print()


# Driver Code
N = 5
print("The number is : ",end="")
digitsNum(N)

# This code is contributed by Gitanjali.

C#

// C# program to find smallest 
// number to find smallest number 
// with N as sum of digits and 
// divisible by 10^N.
using System;

class GFG 
{

static void digitsNum(int N)
{
    // If N = 0 the string will be 0 
    if (N == 0) 
Console.Write("0");
        
    
    // If n is not perfectly divisible 
    // by 9 output the remainder 
    if (N % 9 != 0) 
        Console.Write((N % 9));
    
    
    // Print 9 N/9 times 
    for (int i = 1; i <= (N / 9); ++i) 
        Console.Write("9");
        
    
    // Append N zero's to the number so 
    // as to make it divisible by 10^N )
    for (int i = 1; i <= N; ++i) 
        Console.Write("0");
        Console.WriteLine("" );
    
}

    // Driver Code
    public static void Main ()
    {
    int N = 5;
    Console.Write("The number is : ");
    digitsNum(N);
    }
} 

// This code is contributed by vt_m


PHP


<?php
// PHP program to find smallest 
// number to find smallest number 
// with N as sum of digits and 
// divisible by 10^N.

function digitsNum($N)
{
    // If N = 0 the string will be 0 
    if ($N == 0) 
        echo "0\n";
    
    // If n is not perfectly divisible 
    // by 9 output the remainder
    if ($N % 9 != 0) 
        echo ($N % 9);
    
    // Print 9 N/9 times
    for ( $i = 1; $i <= ($N / 9); ++$i) 
        echo "9";
    
    // Append N zero's to the number so 
    // as to make it divisible by 10^N 
    for ($i = 1; $i <= $N; ++$i) 
        echo "0";
    
    echo "\n";
}

// Driver Code
$N = 5;
echo "The number is : ";
digitsNum($N);

// This code is contributed by ajit.
?>

Output :

The number is : 500000

Time Complexity : O(N)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


Improved By : jit_t




Recommended Posts:



2.3 Average Difficulty : 2.3/5.0
Based on 6 vote(s)






User Actions