Find the smallest number whose sum of digits is N

Given a positive integers N, the task is to find the smallest number whose sum of digits is N.
Example: 
 

Input: N = 10
Output: 19
Explanation:
1 + 9 = 10 = N

Input: N = 18
Output: 99
Explanation:
9 + 9 = 18 = N



 

Naive Approach: 
 

  • A Naive approach is to run a loop of i starting from 0 and find Sum of digits of i and check if it’s equal to N or not. 
     

Below is the implementation of the above approach.
 

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find the smallest
// number whose sum of digits is also N
#include <iostream>
#include <math.h>
using namespace std;
 
// Function to get sum of digits
int getSum(int n)
{
    int sum = 0;
    while (n != 0) {
        sum = sum + n % 10;
        n = n / 10;
    }
    return sum;
}
 
// Function to find the smallest
// number whose sum of digits is also N
void smallestNumber(int N)
{
    int i = 1;
    while (1) {
        // Checking if number has
        // sum of digits = N
        if (getSum(i) == N) {
            cout << i;
            break;
        }
        i++;
    }
}
 
// Driver code
int main()
{
    int N = 10;
    smallestNumber(N);
 
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find the smallest
// number whose sum of digits is also N
class GFG{
 
// Function to get sum of digits
static int getSum(int n)
{
    int sum = 0;
    while (n != 0)
    {
        sum = sum + n % 10;
        n = n / 10;
    }
    return sum;
}
 
// Function to find the smallest
// number whose sum of digits is also N
static void smallestNumber(int N)
{
    int i = 1;
    while (1 != 0)
    {
        // Checking if number has
        // sum of digits = N
        if (getSum(i) == N)
        {
            System.out.print(i);
            break;
        }
        i++;
    }
}
 
// Driver code
public static void main(String[] args)
{
    int N = 10;
    smallestNumber(N);
}
}
 
// This code is contributed
// by shivanisinghss2110

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find the smallest
# number whose sum of digits is also N
 
# Function to get sum of digits
def getSum(n):
 
    sum1 = 0;
    while (n != 0):
        sum1 = sum1 + n % 10;
        n = n // 10;
     
    return sum1;
 
# Function to find the smallest
# number whose sum of digits is also N
def smallestNumber(N):
 
    i = 1;
    while (1):
        # Checking if number has
        # sum of digits = N
        if (getSum(i) == N):
            print(i);
            break;
         
        i += 1;
     
# Driver code
N = 10;
smallestNumber(N);
 
# This code is contributed by Code_Mech

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find the smallest
// number whose sum of digits is also N
using System;
 
class GFG{
 
// Function to get sum of digits
static int getSum(int n)
{
    int sum = 0;
    while (n != 0)
    {
        sum = sum + n % 10;
        n = n / 10;
    }
    return sum;
}
 
// Function to find the smallest
// number whose sum of digits is also N
static void smallestNumber(int N)
{
    int i = 1;
    while (1 != 0)
    {
         
        // Checking if number has
        // sum of digits = N
        if (getSum(i) == N)
        {
            Console.Write(i);
            break;
        }
        i++;
    }
}
 
// Driver code
public static void Main(String[] args)
{
    int N = 10;
     
    smallestNumber(N);
}
}
 
// This code is contributed by Amit Katiyar

chevron_right


Output



19

Time Complexity: O(N).
Efficient Approach: 
 

  • An efficient approach to this problem is an observation. Let’s see some examples. 
    • If N = 10, then ans = 19
    • If N = 20, then ans = 299
    • If N = 30, then ans = 3999
  • So, it is clear that the answer will have all digits as 9 except the first one so that we get the smallest number.
  • So, the Nth term will be =

(N \bmod 9 + 1) * 10^\frac{N}{9} - 1

Below is the implementation of the above approach 
 

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find the smallest
// number whose sum of digits is also N
#include <iostream>
#include <math.h>
using namespace std;
 
// Function to find the smallest
// number whose sum of digits is also N
void smallestNumber(int N)
{
    cout << (N % 9 + 1)
                    * pow(10, (N / 9))
                - 1;
}
 
// Driver code
int main()
{
    int N = 10;
    smallestNumber(N);
 
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find the smallest
// number whose sum of digits is also N
class GFG{
  
// Function to find the smallest
// number whose sum of digits is also N
static void smallestNumber(int N)
{
    System.out.print((N % 9 + 1) *
            Math.pow(10, (N / 9)) - 1);
}
  
// Driver code
public static void main(String[] args)
{
    int N = 10;
    smallestNumber(N);
}
}
 
// This code is contributed by sapnasingh4991

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find the smallest
# number whose sum of digits is also N
 
# Function to find the smallest
# number whose sum of digits is also N
def smallestNumber(N):
 
    print((N % 9 + 1) * pow(10, (N // 9)) - 1)
 
# Driver code
N = 10
smallestNumber(N)
 
# This code is contributed by Code_Mech

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find the smallest
// number whose sum of digits is also N
using System;
class GFG{
 
// Function to find the smallest
// number whose sum of digits is also N
static void smallestNumber(int N)
{
    Console.WriteLine((N % 9 + 1) *
             Math.Pow(10, (N / 9)) - 1);
}
 
// Driver code
public static void Main()
{
    int N = 10;
     
    smallestNumber(N);
}
}
 
// This code is contributed by Ritik Bansal

chevron_right


Output

19

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.