Given a non-negative number n. The problem is to find the smallest number k such that the product of digits of k is equal to n. If no such number k can be formed then print “-1”.
Input : 100 Output : 455 4*5*5 = 100 and 455 is the smallest possible number. Input : 26 Output : -1
Source: Asked in Amazon Interview
Approach: For each i = 9 to 2, repeatedly divide n by i until it cannot be further divided or the list of numbers from 9 to 2 gets finished. Also, in the process of division push each digit i onto the stack which divides n completely. After the above process gets completed check whether n == 1 or not. If not, then print “-1”, else form the number k using the digits from the stack containing the digits in the same sequence as popped from the stack.
Time Complexity: O(num), where num is the size of the stack.
Space Complexity: O(num), where num is the size of the stack.
We can store the required number k in string for large numbers.
This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.
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.
- Find smallest number with given number of digits and sum of digits
- Find smallest number with given number of digits and sum of digits under given constraints
- Smallest number with given sum of digits and sum of square of digits
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- Smallest number greater than or equal to X whose sum of digits is divisible by Y
- Smallest number greater than Y with sum of digits equal to X
- Smallest number greater than or equal to N using only digits 1 to K
- Check if product of digits of a number at even and odd places is equal
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Minimum digits to be removed to make either all digits or alternating digits same
- Numbers with sum of digits equal to the sum of digits of its all prime factor
- Find maximum product of digits among numbers less than or equal to N
- Find a number x such that sum of x and its digits is equal to given n.
- Smallest x such that 1*n, 2*n, ... x*n have all digits from 1 to 9
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Count different numbers that can be generated such that there digits sum is equal to 'n'
- Find the minimum positive integer such that it is divisible by A and sum of its digits is equal to B
- Find the first N integers such that the sum of their digits is equal to 10
- Count of pairs upto N such whose LCM is not equal to their product for Q queries