Smallest x such that 1*n, 2*n, … x*n have all digits from 1 to 9
Given a positive number n. We need to find x such that 1*n, 2*n, 3*n…..x*n gives all 10 digits at least once. If no such x is possible print -1.
Examples:
Input : n = 1692 Output : 3 Explanation: n = 1692, we got the digits- 1, 2, 6, 9 2*n = 3384, we got the digits- 1, 2, 3, 4, 6, 8, 9. 3*n = 5076, we got the digits- 1, 2, 3, 4, 5, 6, 7, 8, 9. At this step we got all the digits at least once. Therefore our answer is 3. Input : 1 Output : 10 Input : 0 Output :-1
The idea used here is simple. We start from 1 and keep multiplying with n till we do not get all the 10 digits at least once. In order to keep track of all the digits coming at each iteration we use an temporary array of size 10 initially having all zeroes. Whenever we got a digit first time we will initialize its index in array with 1. When all digits are visited once, we are done.
Following is the implementation of it.
C++
// CPP program to find x such that 1*n, 2*n, 3*n // ...x * n have all digits from 1 to 9 at least // once #include <bits/stdc++.h> using namespace std; // Returns smallest value x such that 1*n, 2*n, // 3*n ...x * n have all digits from 1 to 9 at // least once int smallestX(int n) { // taking temporary array and variable. int temp[10] = { 0 }; if (n == 0) return -1; // iterate till we get all the 10 digits // at least once int count = 0, x = 0; for (x = 1; count < 10; x++) { int y = x * n; // checking all the digits while (y) { if (temp[y % 10] == false) { count++; temp[y % 10] = true; } y /= 10; } } return x - 1; } // driver function int main() { int n = 5; cout <<smallestX(n); return 0; } |
Java
// Java program to find x such // that 1*n, 2*n, 3*n...x * n // have all digits from 1 to 9 // at least once import java.io.*; import java.util.*; class GFG { // Returns smallest value x // such that 1*n, 2*n, 3*n // ...x * n have all digits // from 1 to 9 at least once public static int smallestX(int n) { // taking temporary // array and variable. int[] temp = new int[10]; for(int i = 0; i < 10; i++) temp[i] = 0; if (n == 0) return -1; // iterate till we get // all the 10 digits // at least once int count = 0, x = 0; for (x = 1; count < 10; x++) { int y = x * n; // checking all // the digits while (y > 0) { if (temp[y % 10] == 0) { count++; temp[y % 10] = 1; } y /= 10; } } return x - 1; } // Driver Code public static void main(String args[]) { int n = 5; System.out.print(smallestX(n)); } } // This code is contributed // by Akanksha Rai(Abby_akku) |
Python3
# Python3 program to find x such # that 1*n, 2*n, 3*n ...x * n # have all digits from 1 to 9 # at least once # Returns smallest value x such # that 1*n, 2*n, 3*n ...x * n # have all digits from 1 to 9 # at least once def smallestX(n): # taking temporary # array and variable. temp = [0]*10 if (n == 0): return -1 # iterate till we get # all the 10 digits # at least once count = 0 x = 1 while(count < 10): y = x * n # checking all # the digits while (y>0): if (temp[y % 10] == 0): count+=1 temp[y % 10] = 1 y = int(y / 10) x+=1 return x - 1 # Driver code if __name__=='__main__': n = 5 print(smallestX(n)) # This code is contributed # by mits |
C#
// C# program to find x such // that 1*n, 2*n, 3*n...x * n // have all digits from 1 to 9 // at least once using System; class GFG { // Returns smallest value x // such that 1*n, 2*n, 3*n // ...x * n have all digits // from 1 to 9 at least once public static int smallestX(int n) { // taking temporary // array and variable. int[] temp = new int[10]; for(int i = 0; i < 10; i++) temp[i] = 0; if (n == 0) return -1; // iterate till we get // all the 10 digits // at least once int count = 0, x = 0; for (x = 1; count < 10; x++) { int y = x * n; // checking all the digits while (y > 0) { if (temp[y % 10] == 0) { count++; temp[y % 10] = 1; } y /= 10; } } return x - 1; } // dDriver Code static void Main() { int n = 5; Console.Write(smallestX(n)); } } // This code is contributed by mits |
PHP
<?php // PHP program to find x such // that 1*n, 2*n, 3*n ...x * n // have all digits from 1 to 9 // at least once // Returns smallest value x such // that 1*n, 2*n, 3*n ...x * n // have all digits from 1 to 9 // at least once function smallestX($n) { // taking temporary // array and variable. $temp = array_fill(0, 10, false); if ($n == 0) return -1; // iterate till we get // all the 10 digits // at least once $count = 0; $x = 0; for ($x = 1; $count < 10; $x++) { $y = $x * $n; // checking all // the digits while ($y) { if ($temp[$y % 10] == false) { $count++; $temp[$y % 10] = true; } $y = (int)($y / 10); } } return $x - 1; } // Driver code $n = 5; echo smallestX($n); // This code is contributed // by mits ?> |
Output:
18
This article is contributed by Saloni Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Smallest number with given sum of digits and sum of square of digits
- Smallest odd digits number not less than N
- Smallest Even number with N digits
- Smallest odd number with N digits
- Smallest even digits number not less than N
- Smallest number with at least n digits in factorial
- Smallest multiple of 3 which consists of three given non-zero digits
- Find the kth smallest number with sum of digits as m
- Smallest number with sum of digits as N and divisible by 10^N
- Smallest and Largest Palindrome with N Digits
- Smallest multiple of N formed using the given set of digits
- Find smallest number with given number of digits and sum of digits
- Smallest multiple of a given number made of digits 0 and 9 only
- Smallest number k such that the product of digits of k is equal to n
- Find the smallest positive number which can not be represented by given digits



