Minimum Factorization - Problem

Given a positive integer num, return the smallest positive integer x whose multiplication of each digit equals num.

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

Note: The result must be composed of digits 2-9 only (no 0 or 1), and we want the smallest possible number.

Input & Output

Example 1 — Basic Factorization
$ Input: num = 48
Output: 68
💡 Note: 48 = 8 × 6. The digits are [8, 6]. Arranging in ascending order gives 68.
Example 2 — Single Digit
$ Input: num = 9
Output: 9
💡 Note: 9 is already a single digit, so the answer is 9 itself.
Example 3 — No Valid Factorization
$ Input: num = 11
Output: 0
💡 Note: 11 is prime and cannot be factored using digits 2-9, so return 0.

Constraints

  • 1 ≤ num ≤ 231 - 1
  • The result must fit in a 32-bit signed integer

Visualization

Tap to expand
Minimum Factorization - Greedy Approach INPUT Positive Integer 48 Find smallest x where: digit1 * digit2 * ... = 48 Constraints: - Digits must be 2-9 only - Result fits in 32-bit int Valid digits: 2 3 4 5 6 7 8 ALGORITHM STEPS 1 Try largest digits first Start from 9 down to 2 2 Check divisibility If num % digit == 0, use it 3 Collect digits Add to result, update num 4 Reverse result Sort ascending for min Trace for num = 48 num try div? digits 48 9 NO [] 48 8 OK [8] 6 9,8,7 NO [8] 6 6 OK [8,6] 1 done -- [6,8] FINAL RESULT Smallest Number: 68 Verification: 6 x 8 = 48 OK - Correct! Why 68? Possible combinations: 2x2x2x6 = 2226 2x4x6 = 246 6x8 = 68 (smallest!) Key Insight: To get the SMALLEST number, we need FEWER digits. Larger factors (8,9) create fewer digits than smaller ones (2,3). So we greedily try dividing by 9, then 8, down to 2. Reverse collected digits to form ascending order (smallest digits first), giving the minimum result. TutorialsPoint - Minimum Factorization | Greedy Factorization Approach
Asked in
Google 15 Amazon 12 Facebook 8
18.5K Views
Medium Frequency
~25 min Avg. Time
456 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen