625. Minimum Factorization


Given a positive integer a, find the smallest positive integer b whose multiplication of each digit equals to a.

If there is no answer or the answer is not fit in 32-bit signed integer, then return 0.

Example 1
Input:

48 
Output:
68

Example 2
Input:

15
Output:
35


Solution


Approach #1 Brute Force [Time Limit Exceeded]

The simplest solution is to consider every possible 32-bit number starting from 1 which satisfies the given criteria. To check this, we obtain each individual digit of every such number and check if their product is equal to the given number . As soon as such a number is found, we return the same. If no such 32-bit number is found, we return a 0 value.

Complexity Analysis

  • Time complexity : . In case of prime numbers loop can go upto this large number.

  • Space complexity : . Constant space is used.


Approach #2 Better Brute Force [Time Limit Exceeded]

Algorithm

Instead of considering every possible number from the total search space, we can do the search in a smarter way. We can start putting the numbers from 9 to 2 at the ones position and keep on proceeding towards more significant places. For every number currently generated, we can check if the product of its digits exceeds the given number . If so, there is no point in appending more digitas to this number. Thus, we can change the composition of the number generated till now and continue the checking process.

For doing this, we make use of a recursive function search(), which takes the number generated till now, (as a string) as one of its arguments along with the number to be appended next as the as a prefix as one of the other arguments. We can note that to obtain the smallest possible number, we need to try to put the largest number(which will be one of the factors for constituting the product ) at the least significant position and the smallest one at the most significant position. Thus, we start from the least significant position by trying to place a 9 at this position and then continue by trying to place smaller numbers at this position if the numbers generated by the previous arrangements fail. If some arrangement leads to a product of digits not larger than , we continue with placing digits, equal to or smaller than the last digit placed, at the more significant positions.

The following animation illustrates the recursive process:

!?!../Documents/625_Minimum_Factorization.json:1000,563!?!

Complexity Analysis

  • Time complexity : . Here refers to total number of combinations.

  • Space complexity : . In worst case, depth of recursion tree can go upto the .


Approach #3 Using Factorization[Accepted]

Algorithm

We know that the final number generated, , should be such that its digits should have a product equal to the given number . In other words, the digits of will be the factors of the given number . Thus, our problem reduces to finding the factors(not necessarily prime) of and finding their smallest possible arrangement. Thus, we start with trying with the largest possible factor , obtain as many such counts of this factor as possible in and place such factors obtianed at its least significant positions. Then, we go on decrementing the number currently considered as the possible factor and if it is a factor, we keep on placing it at relatively more significant positions in . We go on getting such factors till we are done considering all the numbers from 9 to 2. At the end, gives the required result.

Complexity Analysis

  • Time complexity : . Outer loop will iterate only 8 times, while inner loop takes for particular .

  • Space complexity : . Constant space is used.


Analysis written by: @vinod23