This website contains ALL LeetCode **Premium** problems for
**FREE!!**.

All leaked interview problems are collected from Internet.

All leaked interview problems are collected from Internet.

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:

48Output:

68

**Example 2**

Input:

15Output:

35

b'

\n## Solution

\n

\n#### Approach #1 Brute Force [Time Limit Exceeded]

\n

\n#### Approach #2 Better Brute Force [Time Limit Exceeded]

\n

\n#### Approach #3 Using Factorization[Accepted]

\n

\n

'
\n\n

\n\n

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.

\n\n**Complexity Analysis**

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

\n \n - \n
Space complexity : . Constant space is used.

\n \n

\n

**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.

\nFor 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:

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

\n\n**Complexity Analysis**

- \n
- \n
Time complexity : . Here refers to total number of combinations.

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

\n \n

\n

**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.

\n\n**Complexity Analysis**

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

\n \n - \n
Space complexity : . Constant space is used.

\n \n

\n

Analysis written by: @vinod23

\n