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.
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.
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 email@example.com. 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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Minimum digits to be removed to make either all digits or alternating digits same
- Find smallest number with given number of digits and sum of digits
- Smallest number with given sum of digits and sum of square of digits
- Find smallest number with given number of digits and sum of digits under given constraints
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Smallest number k such that the product of digits of k is equal to n
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- Smallest Integer to be inserted to have equal sums
- Check if two numbers have same number of digits
- Find the number of positive integers less than or equal to N that have an odd number of digits
- Smallest number containing all possible N length permutations using digits 0 to D
- Smallest N digit number divisible by all possible prime digits
- Numbers with sum of digits equal to the sum of digits of its all prime factor
- Check if the sum of digits of number is divisible by all of its digits
- Ways to color a skewed tree such that parent and child have different colors
- Longest subarray such that adjacent elements have at least one common digit | Set 1
- Color N boxes using M colors such that K boxes have different color from the box on its left
- Ways to paint N paintings such that adjacent paintings don't have same colors