Smallest multiple of a given number made of digits 0 and 9 only
Last Updated :
29 Aug, 2022
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
Explanation: 90 is the smallest number made up
of 9's and 0's which is divisible by 5.
Input : N = 7
Output : X = 9009
Explanation: 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:
C++
// CPP program to find smallest multiple of a
// given number made of digits 0 and 9 only
#include <bits/stdc++.h>
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<string> vec;
/* Preprocessing function to generate all possible
numbers formed by 0 and 9 */
void generateNumbersUtil()
{
// Create an empty queue of strings
queue<string> q;
// enqueue 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<String> vec = new LinkedList<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 LinkedList<String>();
// enqueue 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()
# enqueue 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>();
// enqueue 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)
JavaScript
// JavaScript program to find smallest multiple of
// a given number made of digits 0 and 9 only
let vec = [];
// Preprocessing function to generate
// all possible numbers formed by 0 and 9
function generateNumbersUtil(){
// Create an empty queue of strings
let q = []
// enqueue 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 (var count = MAX_COUNT; count > -1; count--){
s1 = q[0]
q.shift()
// storing the front of queue
// in the vector
vec.push(s1)
s2 = s1
// Append "0" to s1 and enqueue it
s1 += "0"
q.push(s1)
// Append "9" to s2 and enqueue it. Note
// that s2 contains the previous front
s2 += "9"
q.push(s2)
}
}
// function to find smallest number made
// up of only digits 9’s and 0’s, which
// is a multiple of n.
function findSmallestMultiple(n){
// traverse the vector to find
// the smallest multiple of n
for (var i = 0; i < vec.length; i++)
// int is used for string to
// conversion
if (parseInt(vec[i]) % n == 0)
return vec[i]
}
// Driver Code
// Maximum number of numbers
// made of 0 and 9
let 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()
let n = 7
console.log(findSmallestMultiple(n))
// This code is contributed by phasing17
Time Complexity: O(n), where n represents the given integer.
Auxiliary Space: O(10000), no extra space is required, so it is a constant.
Similar Reads
Find second smallest number from sum of digits and number of digits Given the sum of digits as S and the number of digits as D, the task is to find the second smallest number Examples: Input: S = 9, D = 2Output: 27Explanation: 18 is the smallest number possible with sum = 9 and total digits = 2, Whereas the second smallest is 27. Input: S = 16, D = 3Output: 178Expla
8 min read
Minimum number with digits as 4 and 7 only and given sum Lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. What minimum lucky number has the sum of digits equal to n. Examples: Input : sum = 11 Output : 47 Sum of digits in 47 is 11 and 47 is the smallest number with given sum. Input : sum = 10 Out
6 min read
Smallest number by rearranging digits of a given number Find the Smallest number (Not leading Zeros) which can be obtained by rearranging the digits of a given number. Examples: Input: n = 846903Output: 304689Input: n = 55010Output: 10055Input: n = -40505Output: -55400Steps to find the smallest number. Count the frequency of each digit in the number.If i
7 min read
Smallest integer with digit sum M and multiple of N Given two positive integers N and M, the task is to find the smallest positive integer which is divisible by N and whose digit sum is M. Print -1 if no such integer exists within the range of int. Examples: Input: N = 13, M = 32 Output: 8879 8879 is divisible by 13 and its Sum of digits of 8879 is 8
5 min read
Smallest multiple of N formed using the given set of digits Given a set of digits S and an integer N, the task is to find the smallest positive integer if exists which contains only the digits from S and is a multiple of N. Note that the digits from the set can be used multiple times. Examples: Input: S[] = {5, 2, 3}, N = 12 Output: 252 We can observe that 2
10 min read
Smallest multiple of 3 which consists of three given non-zero digits Given three non-zero digits 0 < A, B, C < 9. The task is to find the smallest number divisible by 3 all of whose digits are in set {A, B, C}. Note: It is not necessary to include all three digits. The result can be A, AA, AB, CCA etc.Examples: Input: A = 2, B = 4, C = 7 Output: 24 24 is the mi
7 min read
Find the smallest number whose digits multiply to a given number n Given a number 'n', find the smallest number 'p' such that if we multiply all digits of 'p', we get 'n'. The result 'p' should have minimum two digits.Examples: Input: n = 36 Output: p = 49 // Note that 4*9 = 36 and 49 is the smallest such number Input: n = 100 Output: p = 455 // Note that 4*5*5 = 1
8 min read
Find smallest number with given digits and sum of digits Given two positive integers P and Q, find the minimum integer containing only digits P and Q such that the sum of the digits of the integer is N. Example: Input: N = 11, P = 4, Q = 7 Output: 47Explanation: There are two possible integers that can be formed from 4 and 7 such that their sum is 11 i.e.
9 min read
Smallest number with given sum of digits and sum of square of digits Given the sum of digits a and sum of the square of digits b . Find the smallest number with the given sum of digits and the sum of the square of digits. The number should not contain more than 100 digits. Print -1 if no such number exists or if the number of digits is more than 100.Examples: Input :
15+ min read
Find the Largest number with given number of digits and sum of digits Given an integer s and d, The task is to find the largest number with given digit sum s and the number of digits d. Examples: Input: s = 9, d = 2Output: 90 Input: s = 20, d = 3Output: 992 Recommended PracticeLargest number possibleTry It! Naive Approach: Consider all m digit numbers and keep a max v
13 min read