# Smallest multiple of a given number made of digits 0 and 9 only

We are given an integer N. We need to write a program to find the least positive integer X made up of only digits 9’s and 0’s, such that, X is a multiple of N.

Note: It is assumed that the value of X will not exceed 106.

Examples:

```Input : N = 5
Output : X = 90
Exaplanation: 90 is the smallest number made up
of 9's and 0's which is divisible by 5.

Input : N = 7
Output : X = 9009
Exaplanation: 9009 is smallest number made up
of 9's and 0's which is divisible by 7.
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

The idea to solve this problem is to generate and store all of the numbers which can be formed using digits 0 & 9. Then find the smallest number among these generated number which is divisible by N.

We will use the method of generating binary numbers to generate all numbers which can be formed by using digits 0 & 9.

Below is the implementation of above idea:

## C++

 `// CPP program to find smallest multiple of a  ` `// given number made of digits 0 and 9 only ` `#include ` `using` `namespace` `std; ` ` `  `// Maximum number of numbers made of 0 and 9 ` `#define MAX_COUNT 10000 ` ` `  `// vector to store all numbers that can be formed ` `// using digits 0 and 9 and are less than 10^5 ` `vector vec; ` ` `  `/* Preprocessing function to generate all possible  ` `   ``numbers formed by 0 and 9 */` `void` `generateNumbersUtil() ` `{    ` `    ``// Create an empty queue of strings ` `    ``queue q; ` `         `  `    ``// enque the first number ` `    ``q.push(``"9"``); ` `     `  `    ``// This loops is like BFS of a tree with 9 as root ` `    ``// 0 as left child and 9 as right child and so on ` `    ``for` `(``int` `count = MAX_COUNT; count > 0; count--) ` `    ``{ ` `        ``string s1 = q.front(); ` `        ``q.pop(); ` `         `  `        ``// storing the front of queue in the vector ` `        ``vec.push_back(s1); ` `         `  `        ``string s2 = s1; ` `         `  `        ``// Append "0" to s1 and enqueue it ` `        ``q.push(s1.append(``"0"``)); ` `         `  `        ``// Append "9" to s2 and enqueue it. Note that ` `        ``// s2 contains the previous front ` `        ``q.push(s2.append(``"9"``)); ` `    ``} ` `} ` ` `  `// function to find smallest number made up of only  ` `// digits 9’s and 0’s, which is a multiple of n. ` `string findSmallestMultiple(``int` `n) ` `{    ` `    ``// traverse the vector to find the smallest ` `    ``// multiple of n ` `    ``for` `(``int` `i = 0; i < vec.size(); i++) ` ` `  `        ``// stoi() is used for string to int conversion ` `        ``if` `(stoi(vec[i])%n == 0)  ` `            ``return` `vec[i];         ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``generateNumbersUtil();     ` `    ``int` `n = 7;     ` `    ``cout << findSmallestMultiple(n);     ` `    ``return` `0; ` `} `

## Java

 `// Java program to find smallest  ` `// multiple of a given number  ` `// made of digits 0 and 9 only ` `import` `java.util.*; ` ` `  `class` `GFG  ` `{ ` ` `  `    ``// Maximum number of  ` `    ``// numbers made of 0 and 9 ` `    ``static` `int` `MAX_COUNT = ``10000``; ` ` `  `    ``// vector to store all numbers  ` `    ``// that can be formed using  ` `    ``// digits 0 and 9 and are  ` `    ``// less than 10^5 ` `    ``static` `List vec = ``new` `LinkedList(); ` ` `  `    ``/* Preprocessing function  ` `    ``to generate all possible  ` `    ``numbers formed by 0 and 9 */` `    ``static` `void` `generateNumbersUtil()  ` `    ``{ ` `        ``// Create an empty ` `        ``// queue of Strings ` `        ``Queue q = ``new` `LinkedList(); ` ` `  `        ``// enque the  ` `        ``// first number ` `        ``q.add(``"9"``); ` ` `  `        ``// This loops is like BFS of ` `        ``// a tree with 9 as root ` `        ``// 0 as left child and 9 as  ` `        ``// right child and so on ` `        ``for` `(``int` `count = MAX_COUNT; ` `                ``count > ``0``; count--) ` `        ``{ ` `            ``String s1 = q.peek(); ` `            ``q.remove(); ` ` `  `            ``// storing the Peek of ` `            ``// queue in the vector ` `            ``vec.add(s1); ` ` `  `            ``String s2 = s1; ` ` `  `            ``// Append "0" to s1 ` `            ``// and enqueue it ` `            ``q.add(s1 + ``"0"``); ` ` `  `            ``// Append "9" to s2 and  ` `            ``// enqueue it. Note that ` `            ``// s2 contains the previous Peek ` `            ``q.add(s2 + ``"9"``); ` `        ``} ` `    ``} ` ` `  `    ``// function to find smallest  ` `    ``// number made up of only  ` `    ``// digits 9's and 0's, which  ` `    ``// is a multiple of n. ` `    ``static` `String findSmallestMultiple(``int` `n)  ` `    ``{ ` `        ``// traverse the vector  ` `        ``// to find the smallest  ` `        ``// multiple of n ` `        ``for` `(``int` `i = ``0``; i < vec.size(); i++) ``// stoi() is used for  ` `        ``// String to int conversion ` `        ``{ ` `            ``if` `(Integer.parseInt(vec.get(i)) % n == ``0``)  ` `            ``{ ` `                ``return` `vec.get(i); ` `            ``} ` `        ``} ` `        ``return` `""``; ` `    ``} ` ` `  `    ``// Driver Code ` `    ``public` `static` `void` `main(String[] args)  ` `    ``{ ` `        ``generateNumbersUtil(); ` `        ``int` `n = ``7``; ` `        ``System.out.println(findSmallestMultiple(n)); ` `    ``} ` `} ` ` `  `// This code is contributed by Rajput-Ji `

## Python3

 `# Python3 program to find smallest multiple of ` `# a given number made of digits 0 and 9 only  ` `from` `queue ``import` `Queue ` ` `  `# Preprocessing function to generate  ` `# all possible numbers formed by 0 and 9  ` `def` `generateNumbersUtil(): ` `    ``global` `vec ` `     `  `    ``# Create an empty queue of strings  ` `    ``q ``=` `Queue() ` `         `  `    ``# enque the first number  ` `    ``q.put(``"9"``)  ` `     `  `    ``# This loops is like BFS of a tree  ` `    ``# with 9 as root, 0 as left child ` `    ``# and 9 as right child and so on ` `    ``for` `count ``in` `range``(MAX_COUNT, ``-``1``, ``-``1``): ` `        ``s1 ``=` `q.queue[``0``]  ` `        ``q.get() ` `         `  `        ``# storing the front of queue  ` `        ``# in the vector  ` `        ``vec.append(s1)  ` `         `  `        ``s2 ``=` `s1  ` `         `  `        ``# Append "0" to s1 and enqueue it ` `        ``s1 ``+``=` `"0"` `        ``q.put(s1)  ` `         `  `        ``# Append "9" to s2 and enqueue it. Note  ` `        ``# that s2 contains the previous front ` `        ``s2 ``+``=` `"9"` `        ``q.put(s2) ` ` `  `# function to find smallest number made  ` `# up of only digits 9’s and 0’s, which  ` `# is a multiple of n.  ` `def` `findSmallestMultiple(n): ` `    ``global` `vec ` `     `  `    ``# traverse the vector to find  ` `    ``# the smallest multiple of n ` `    ``for` `i ``in` `range``(``len``(vec)): ` ` `  `        ``# int is used for string to ` `        ``# conversion  ` `        ``if` `(``int``(vec[i]) ``%` `n ``=``=` `0``):  ` `            ``return` `vec[i]  ` ` `  `# Driver Code  ` ` `  `# Maximum number of numbers ` `# made of 0 and 9  ` `MAX_COUNT ``=` `10000` ` `  `# stack to store all numbers that  ` `# can be formed using digits 0 and  ` `# 9 and are less than 10^5  ` `vec ``=` `[]  ` `generateNumbersUtil()      ` `n ``=` `7`     `print``(findSmallestMultiple(n)) ` ` `  `# This code is contributed by PranchalK `

## C#

 `// C# program to find smallest  ` `// multiple of a given number  ` `// made of digits 0 and 9 only ` `using` `System; ` `using` `System.Collections.Generic; ` ` `  `class` `GFG ` `{  ` `    ``// Maximum number of  ` `    ``// numbers made of 0 and 9 ` `    ``static` `int` `MAX_COUNT = 10000; ` `     `  `    ``// vector to store all numbers  ` `    ``// that can be formed using  ` `    ``// digits 0 and 9 and are  ` `    ``// less than 10^5 ` `    ``static` `List<``string``> vec = ``new` `List<``string``>(); ` `     `  `    ``/* Preprocessing function  ` `    ``to generate all possible  ` `    ``numbers formed by 0 and 9 */` `    ``static` `void` `generateNumbersUtil() ` `    ``{  ` `        ``// Create an empty ` `        ``// queue of strings ` `        ``Queue<``string``> q = ``new` `Queue<``string``>(); ` `             `  `        ``// enque the  ` `        ``// first number ` `        ``q.Enqueue(``"9"``); ` `         `  `        ``// This loops is like BFS of ` `        ``// a tree with 9 as root ` `        ``// 0 as left child and 9 as  ` `        ``// right child and so on ` `        ``for` `(``int` `count = MAX_COUNT;  ` `                 ``count > 0; count--) ` `        ``{ ` `            ``string` `s1 = q.Peek(); ` `            ``q.Dequeue(); ` `             `  `            ``// storing the Peek of ` `            ``// queue in the vector ` `            ``vec.Add(s1); ` `             `  `            ``string` `s2 = s1; ` `             `  `            ``// Append "0" to s1 ` `            ``// and enqueue it ` `            ``q.Enqueue(s1 + ``"0"``); ` `             `  `            ``// Append "9" to s2 and  ` `            ``// enqueue it. Note that ` `            ``// s2 contains the previous Peek ` `            ``q.Enqueue(s2 + ``"9"``); ` `        ``} ` `    ``} ` `     `  `    ``// function to find smallest  ` `    ``// number made up of only  ` `    ``// digits 9’s and 0’s, which  ` `    ``// is a multiple of n. ` `    ``static` `string` `findSmallestMultiple(``int` `n) ` `    ``{  ` `        ``// traverse the vector  ` `        ``// to find the smallest  ` `        ``// multiple of n ` `        ``for` `(``int` `i = 0; i < vec.Count; i++) ` `     `  `            ``// stoi() is used for  ` `            ``// string to int conversion ` `            ``if` `(``int``.Parse(vec[i]) % n == 0)  ` `                ``return` `vec[i];      ` `        ``return` `""``; ` `    ``} ` `     `  `    ``// Driver Code ` `    ``static` `void` `Main() ` `    ``{ ` `        ``generateNumbersUtil();  ` `        ``int` `n = 7;  ` `        ``Console.Write(findSmallestMultiple(n));  ` `    ``} ` `} ` ` `  `// This code is contributed by  ` `// Manish Shaw(manishshaw1) `

Output:

```9009
```

Time Complexity: O(n)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

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.

Article Tags :
Practice Tags :

3

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.