Find the smallest binary digit multiple of given number
A decimal number is called a binary digit number if its digits are binary. For example, 102 is not a binary digit number and 101 is.
We are given a decimal number N, we need to find the smallest multiple of N which is a binary digit number,
Examples:
Input : N = 2 Output: 10 Explanation: 10 is a multiple of 2. Note that 5 * 2 = 10 Input : N = 17 Output : 11101 Explanation: 11101 is a multiple of 17. Note that 653 * 17 = 11101
We can solve this problem using BFS, every node of the implicit graph will be a binary digit number and if the number is x, then its next level node will be x0 and x1 (x concatenated with 0 and 1).
In starting, we will push 1 into our queue, which will push 10 and 11 into queue later and so on, after taking the number from the queue we’ll check whether this number is multiple of a given number or not, if yes then return this number as result, this will be our final result because BFS proceeds level by level so the first answer we got will be our smallest answer also.
In the below code binary digit number is treated as a string, because for some number it can be very large and can outside the limit of even long, mod operation on number stored as the string is also implemented.
The main optimization tweak of code is using a set for modular value, if a string with the same mod value has previously occurred we won’t push this new string into our queue. The reason for not pushing new string is explained below,
Let x and y be strings, which gives the same modular value. Let x be the smaller one. let z be another string which when appended to y gives us a number divisible by N. If so, then we can also append this string to x, which is smaller than y, and still get a number divisible by n. So we can safely ignore y, as the smallest result will be obtained via x only.
Implementation:
C++
// C++ code to get the smallest multiple of N with // binary digits only. #include <bits/stdc++.h> using namespace std; // Method return t % N, where t is stored as // a string int mod(string t, int N) { int r = 0; for ( int i = 0; i < t.length(); i++) { r = r * 10 + (t[i] - '0' ); r %= N; } return r; } // method returns smallest multiple which has // binary digits string getMinimumMultipleOfBinaryDigit( int N) { queue<string> q; set< int > visit; string t = "1" ; // In starting push 1 into our queue q.push(t); // loop until queue is not empty while (!q.empty()) { // Take the front number from queue. t = q.front(); q.pop(); // Find remainder of t with respect to N. int rem = mod(t, N); // if remainder is 0 then we have // found our solution if (rem == 0) return t; // If this remainder is not previously seen, // then push t0 and t1 in our queue else if (visit.find(rem) == visit.end()) { visit.insert(rem); q.push(t + "0" ); q.push(t + "1" ); } } } // Driver code to test above methods int main() { int N = 12; cout << getMinimumMultipleOfBinaryDigit(N); return 0; } |
Java
// Java code to get the smallest multiple // of N with binary digits only. import java.util.*; import java.io.*; class GFG{ // Method return t % N, where t is stored as // a string public static int mod(String t, int N) { int r = 0 ; for ( int i = 0 ; i < t.length(); i++) { r = r * 10 + (t.charAt(i) - '0' ); r %= N; } return r; } // method returns smallest multiple which has // binary digits public static String getMinimumMultipleOfBinaryDigit( int N) { Queue<String> q = new LinkedList<String>(); Set<Integer> visit = new HashSet<>(); String t = "1" ; // In starting push 1 into our queue q.add(t); // loop until queue is not empty while (!q.isEmpty()) { // Take the front number from queue. t = q.remove(); // Find remainder of t with respect to N. int rem = mod(t, N); // If remainder is 0 then we have // found our solution if (rem == 0 ) return t; // If this remainder is not previously seen, // then push t0 and t1 in our queue else if (!visit.contains(rem)) { visit.add(rem); q.add(t + "0" ); q.add(t + "1" ); } } return "" ; } // Driver code public static void main (String[] args) { int N = 12 ; System.out.println( getMinimumMultipleOfBinaryDigit(N)); } } // This code is contributed by // Naresh Saharan and Sagar Jangra |
Python3
def getMinimumMultipleOfBinaryDigit(A): # queue for BFS q = [] # set of visited remainders visitedRem = set ([]) t = '1' q.append(t) while q: t = q.pop( 0 ) rem = int (t) % A if rem = = 0 : return t if rem not in visitedRem: visitedRem.add(rem) q.append(t + '0' ) q.append(t + '1' ) # Driver code n = 12 print ( getMinimumMultipleOfBinaryDigit(n)) # This code is contributed # by Jeet9 |
C#
// C# code to get the smallest // multiple of N with binary // digits only. using System; using System.Collections.Generic; class GFG{ // Method return t % N, // where t is stored as // a string public static int mod(String t, int N) { int r = 0; for ( int i = 0; i < t.Length; i++) { r = r * 10 + (t[i] - '0' ); r %= N; } return r; } // method returns smallest // multiple which has // binary digits public static String getMinimumMultipleOfBinaryDigit( int N) { Queue<String> q = new Queue<String>(); HashSet< int > visit = new HashSet< int >(); String t = "1" ; // In starting push 1 // into our queue q.Enqueue(t); // loop until queue // is not empty while (q.Count != 0) { // Take the front number // from queue. t = q.Dequeue(); // Find remainder of t // with respect to N. int rem = mod(t, N); // If remainder is 0 then // we have found our solution if (rem == 0) return t; // If this remainder is not // previously seen, then push // t0 and t1 in our queue else if (!visit.Contains(rem)) { visit.Add(rem); q.Enqueue(t + "0" ); q.Enqueue(t + "1" ); } } return "" ; } // Driver code public static void Main(String[] args) { int N = 12; Console.WriteLine(getMinimumMultipleOfBinaryDigit(N)); } } // This code is contributed by Rajput-Ji |
Javascript
<script> // JavaScript code to get the smallest multiple of N with // binary digits only. // Method return t % N, where t is stored as // a string function mod(t,N) { let r = 0; for (let i = 0; i < t.length; i++) { r = r * 10 + (t[i] - '0' ); r %= N; } return r; } // method returns smallest multiple which has // binary digits function getMinimumMultipleOfBinaryDigit(N) { let q = []; let visit = new Set(); let t = "1" ; // In starting push 1 into our queue q.push(t); // loop until queue is not empty while (q.length) { // Take the front number from queue. t = q[0]; q.shift(); // Find remainder of t with respect to N. let rem = mod(t, N); // if remainder is 0 then we have // found our solution if (rem == 0) return t; // If this remainder is not previously seen, // then push t0 and t1 in our queue else if (visit.has(rem) == false ) { visit.add(rem); q.push(t + "0" ); q.push(t + "1" ); } } } // Driver code to test above methods let N = 12; document.write(getMinimumMultipleOfBinaryDigit(N)); // This code is contributed by shinjanpatra. </script> |
11100
Time Complexity: O(V+E), where the time complexity is equal to the number of elements encountered before arriving at the desired outcome. Here, V is the number of nodes and E is the graph’s edges.
Space Complexity: O(V), that is the number of elements we encounter before arriving at the desired result.
Contact Us