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.
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.
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:
# 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
# Create an empty queue of strings
q = Queue()
# enque the first number
# 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
# storing the front of queue
# in the vector
s2 = s1
# Append “0” to s1 and enqueue it
s1 += “0”
# Append “9” to s2 and enqueue it. Note
# that s2 contains the previous front
s2 += “9”
# function to find smallest number made
# up of only digits 9’s and 0’s, which
# is a multiple of n.
# traverse the vector to find
# the smallest multiple of n
for i in range(len(vec)):
# int is used for string to
if (int(vec[i]) % n == 0):
# 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 = 
n = 7
# This code is contributed by PranchalK
Time Complexity: O(n)
- Smallest multiple of N formed using the given set of digits
- Nth number made up of odd digits only
- Find the n-th number made of even digits only
- Smallest number with given sum of digits and sum of square of digits
- Number of digits in the nth number made of given four digits
- Find the smallest binary digit multiple of given number
- Find smallest number with given number of digits and sum of digits
- Smallest even digits number not less than N
- Smallest odd digits number not less than N
- Smallest number with sum of digits as N and divisible by 10^N
- Find the smallest number whose digits multiply to a given number n
- Smallest number k such that the product of digits of k is equal to n
- Immediate smallest number after re-arranging the digits of a given number
- Smallest number by rearranging digits of a given number
- Get the kth smallest number using the digits of the given number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.