You are given an integer N. You have to find smallest multiple of N which consists of digits 0 and 1 only. Since this multiple could be large, return it in form of a string.
Note:
Returned string should not contain leading zeroes.
For example,
For N = 55, 110 is smallest multiple consisting of digits 0 and 1.
For N = 2, 10 is the answer.
publicclassSolution {publicstaticclassNode {String value;int modN =-1;publicNode(String x,int y) { value = x; modN = y; } }publicStringmultiple(int N) {Deque<Node> queue =newLinkedList<>();queue.addLast(newNode("1",1% N));boolean[] visited =newboolean[N]; visited[1% N] =true;while (!queue.isEmpty()) {Node node =queue.pollFirst();// If we reach a multiple of Nif (node.modN==0)returnnode.value;// option 1 -> put 0 in previous answer's endint s1 = (node.modN*10+0) % N;// option 2 -> put 1 in previous answer's endint s2 = (node.modN*10+1) % N;if (!visited[s1]) {queue.addLast(newNode(node.value+"0", s1)); visited[s1] =true; }if (!visited[s2]) {queue.addLast(newNode(node.value+"1", s2)); visited[s2] =true; } }return""; }}