Smallest number whose digits multiplication is β€˜N’

Moderate
0/80
Average time to solve is 15m
profile
Contributed by
7 upvotes
Asked in companies
OlaHewlett Packard EnterpriseCogoport

Problem statement

You are given a positive integer β€˜N’. The task is to find and return the smallest number, β€˜M’, such that the multiplication of all the digits in β€˜M’ is equal to β€˜N’. If no such β€˜M’ is possible or β€˜M’ cannot fit in a 32-bit signed integer, return 0.

Example:
β€˜N’ = 90

Possible values for β€˜M’ are:
1. 259 (2*5*9 = 90) 
2. 3352 (3*3*5*2 = 90) 
3. 2335 (2*3*3*5 = 90) 
4. 952 (9*5*2 = 90), and so on.
Here, β€˜259’ is the smallest possible β€˜M’. Thus, you should return β€˜259’ as the answer.
Detailed explanation ( Input/output format, Notes, Images )
Input format:
The first line of input contains an integer β€˜T’ which denotes the number of test cases. Then, the β€˜T’ test cases follow.

The first and only line of each test case contains an integer β€˜N’, i.e., the given integer.
Output format:
For every test case, return the smallest possible β€˜M’ value. If no such β€˜M’ is possible or β€˜M’ cannot fit in a 32-bit signed integer, return 0.
Note:
You do not need to print anything; it has already been taken care of. Just implement the function.
Constraints:
1 <= T <= 1000
1 <= N <= 10^9

Time limit: 1 sec

Sample input 1:

2
120
62

Sample output 1:

358
0

Explanation of sample input 1:

Test Case 1:

Possible values for β€˜M’ are:
1. 358 (3*5*8 = 120)
2. 2345 (2*3*4*5 = 120)
3. 22235 (2*2*2*3*5 = 120), and so on.
Here, β€˜358’ is the smallest possible β€˜M’. Thus, you should return β€˜358’ as the answer.

Test Case 2:

The factorization of β€˜62 = 2*31’. As β€˜31’ is a prime number, it cannot have single-digit factors (β€˜0’ to β€˜9’) . Thus, it’s impossible to have an integer β€˜M’, such that the multiplication of its digits is equal to β€˜62’. You should return β€˜0’ as the answer.

Sample input 2:

2
168
180

Sample output 2:

378
459
Hint

Check for all possible β€˜M’ values.

Approaches (2)
Brute force

A simple approach will be to iterate through all possible β€˜M’ values, i.e., from β€˜1’ to β€˜2147483647’ (the largest value that a signed 32-bit integer field can hold), and check if their digit multiplication is equal to β€˜N’.

 

Algorithm:

  • Run a loop where β€˜i’ ranges from β€˜1’ to β€˜2147483647’:
    • Set β€˜CUR = i’. The current β€˜M’ value.
    • Set β€˜MUL = 1’. Use it to store the digit multiplication of β€˜M’.
    • Run a loop until β€˜cur’ is not equal to β€˜0’ to find the digit multiplication of β€˜CUR’:
      • β€˜MUL = MUL*(CUR/10)’. Multiply the digit at one’s place in β€˜CUR’ to β€˜MUL’.
      • β€˜CUR /= 10’. Divide β€˜CUR’ by β€˜10’ to move the next digit in β€˜CUR’ to one’s place.
    • If β€˜MUL’ is equal to β€˜N’, then β€˜i’ is the smallest β€˜M’ value possible:
      • Return β€˜i’ as the answer.
  • Return β€˜0’ as we could not find a valid β€˜M’ value.
Time Complexity

O(M*log(M)), where β€˜M’ is the answer.

 

We iterate through all the integers up to β€˜M’ or β€˜2147483647’ (when β€˜N’ has a prime factor greater than β€˜9’), and in each iteration, we compute the digit multiplication of the current β€˜M’ in β€˜O(log(M))’. Thus, the time complexity is β€˜O(M*log(M))’.

Space Complexity

O(1).

 

Since we are not using any extra space, space complexity is constant.

Code Solution
(100% EXP penalty)
Smallest number whose digits multiplication is β€˜N’
Full screen
Console