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
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++
#include <bits/stdc++.h>
using namespace std;
#define MAX_COUNT 10000
vector<string> vec;
void generateNumbersUtil()
{
queue<string> q;
q.push( "9" );
for ( int count = MAX_COUNT; count > 0; count--)
{
string s1 = q.front();
q.pop();
vec.push_back(s1);
string s2 = s1;
q.push(s1.append( "0" ));
q.push(s2.append( "9" ));
}
}
string findSmallestMultiple( int n)
{
for ( int i = 0; i < vec.size(); i++)
if (stoi(vec[i])%n == 0)
return vec[i];
}
int main()
{
generateNumbersUtil();
int n = 7;
cout << findSmallestMultiple(n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int MAX_COUNT = 10000 ;
static List<String> vec = new LinkedList<String>();
static void generateNumbersUtil()
{
Queue<String> q = new LinkedList<String>();
q.add( "9" );
for ( int count = MAX_COUNT;
count > 0 ; count--)
{
String s1 = q.peek();
q.remove();
vec.add(s1);
String s2 = s1;
q.add(s1 + "0" );
q.add(s2 + "9" );
}
}
static String findSmallestMultiple( int n)
{
for ( int i = 0 ; i < vec.size(); i++)
{
if (Integer.parseInt(vec.get(i)) % n == 0 )
{
return vec.get(i);
}
}
return "" ;
}
public static void main(String[] args)
{
generateNumbersUtil();
int n = 7 ;
System.out.println(findSmallestMultiple(n));
}
}
|
Python3
from queue import Queue
def generateNumbersUtil():
global vec
q = Queue()
q.put( "9" )
for count in range (MAX_COUNT, - 1 , - 1 ):
s1 = q.queue[ 0 ]
q.get()
vec.append(s1)
s2 = s1
s1 + = "0"
q.put(s1)
s2 + = "9"
q.put(s2)
def findSmallestMultiple(n):
global vec
for i in range ( len (vec)):
if ( int (vec[i]) % n = = 0 ):
return vec[i]
MAX_COUNT = 10000
vec = []
generateNumbersUtil()
n = 7
print (findSmallestMultiple(n))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int MAX_COUNT = 10000;
static List< string > vec = new List< string >();
static void generateNumbersUtil()
{
Queue< string > q = new Queue< string >();
q.Enqueue( "9" );
for ( int count = MAX_COUNT;
count > 0; count--)
{
string s1 = q.Peek();
q.Dequeue();
vec.Add(s1);
string s2 = s1;
q.Enqueue(s1 + "0" );
q.Enqueue(s2 + "9" );
}
}
static string findSmallestMultiple( int n)
{
for ( int i = 0; i < vec.Count; i++)
if ( int .Parse(vec[i]) % n == 0)
return vec[i];
return "" ;
}
static void Main()
{
generateNumbersUtil();
int n = 7;
Console.Write(findSmallestMultiple(n));
}
}
|
Javascript
let vec = [];
function generateNumbersUtil(){
let q = []
q.push( "9" )
for ( var count = MAX_COUNT; count > -1; count--){
s1 = q[0]
q.shift()
vec.push(s1)
s2 = s1
s1 += "0"
q.push(s1)
s2 += "9"
q.push(s2)
}
}
function findSmallestMultiple(n){
for ( var i = 0; i < vec.length; i++)
if (parseInt(vec[i]) % n == 0)
return vec[i]
}
let MAX_COUNT = 10000
vec = []
generateNumbersUtil()
let n = 7
console.log(findSmallestMultiple(n))
|
Time Complexity: O(n), where n represents the given integer.
Auxiliary Space: O(10000), no extra space is required, so it is a constant.
Last Updated :
29 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...