Given a number ‘n’, find the smallest number ‘p’ such that if we multiply all digits of ‘p’, we get ‘n’. The result ‘p’ should have minimum two digits.
Input: n = 36 Output: p = 49 // Note that 4*9 = 36 and 49 is the smallest such number Input: n = 100 Output: p = 455 // Note that 4*5*5 = 100 and 455 is the smallest such number Input: n = 1 Output:p = 11 // Note that 1*1 = 1 Input: n = 13 Output: Not Possible
For a given n, following are the two cases to be considered.
Case 1: n < 10 When n is smaller than 10, the output is always n+10. For example for n = 7, the output is 17. For n = 9, output is 19.
Case 2: n >= 10 Find all factors of n which are between 2 and 9 (both inclusive). The idea is to start searching from 9 so that the number of digits in the result is minimized. For example, 9 is preferred over 33 and 8 is preferred over 24.
Store all found factors in an array. The array would contain digits in non-increasing order, so finally print the array in reverse order.
Following is the implementation of above concept.
17 49 Not possible 455
This article is contributed by Ashish Bansal. 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 under given constraints
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Find smallest number with given number of digits and sum of digits
- Find the smallest number whose sum of digits is N
- Smallest number to multiply to convert floating point to natural
- Smallest number with given sum of digits and sum of square of digits
- Smallest N digit number whose sum of square of digits is a Perfect Square
- Smallest number greater than or equal to X whose sum of digits is divisible by Y
- Smallest number whose sum of digits is square of N
- Smallest number whose square has N digits
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Queries to multiply the given subarray with given number X and print sum
- Minimum digits to be removed to make either all digits or alternating digits same
- Find smallest possible Number from a given large Number with same count of digits
- Find smallest number formed by inverting digits of given number N
- Find the Largest number with given number of digits and sum of digits
- Multiply the given number by 2 such that it is divisible by 10
- Find the smallest positive number which can not be represented by given digits
- Find the average of k digits from the beginning and l digits from the end of the given number
- Maximize the given number by replacing a segment of digits with the alternate digits given