Smallest Number With Given Digit Product - Problem

Given a positive integer n, return a string representing the smallest positive integer such that the product of its digits is equal to n.

If no such number exists, return "-1".

Note: The returned number should be as small as possible, which means it should have the fewest digits, and among numbers with the same number of digits, it should be lexicographically smallest.

Input & Output

Example 1 — Basic Case
$ Input: n = 12
Output: 26
💡 Note: We need digits that multiply to 12. Using greedy approach: 12 = 6 × 2. Sort digits: [2,6] → "26"
Example 2 — Single Digit
$ Input: n = 1
Output: 1
💡 Note: Special case: the smallest number with digit product 1 is 1 itself
Example 3 — Impossible Case
$ Input: n = 11
Output: -1
💡 Note: 11 is prime and > 9, so it cannot be expressed as a product of single digits (2-9)

Constraints

  • 1 ≤ n ≤ 109

Visualization

Tap to expand
Find Smallest Number with Digit Product = 12Target126212 = 6 × 2Smallest26Sort digits ascending: [2,6] → "26"✓ Verification: 2 × 6 = 12
Understanding the Visualization
1
Input
Target product n = 12
2
Factorize
Break into digit factors: 12 = 6 × 2
3
Output
Smallest arrangement: "26"
Key Takeaway
🎯 Key Insight: Use greedy factorization with largest digits first, then sort ascending for the smallest possible number
Asked in
Google 15 Amazon 12 Microsoft 8
23.0K Views
Medium Frequency
~15 min Avg. Time
890 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