Given a positive integer product, find the smallest positive integer that has a digit product equal to product.
If there is no such integer, return -1.
Example 1:
Input: product = 12
Output: 26
Example 2:
Input: product = 19
Output: -1
Example 3:
Input: product = 1
Output: 1
Constraints:
1 <= product <= 109When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method involves checking every possible number to see if its digits multiply to the target product. We keep generating numbers, calculating their digit product, and seeing if it matches the required product. We continue until we find the smallest such number.
Here's how the algorithm would work step-by-step:
def smallest_number_with_product_brute_force(target_product):
current_number = 1
while True:
product_of_digits = 1
temp_number = current_number
while temp_number > 0:
digit = temp_number % 10
product_of_digits *= digit
temp_number //= 10
# Check if the product matches the target.
if product_of_digits == target_product:
# Found the smallest number.
return current_number
current_number += 1
def smallest_number_with_given_digit_product(target_product):
if target_product == 0:
return 10
if target_product == 1:
return 1
# Utilize brute force implementation to find the number
result = smallest_number_with_product_brute_force(target_product)
return resultThe problem asks us to find the smallest number whose digits, when multiplied together, equal a given product. The key is to build the number from its digits starting from the ones place and working our way up, prioritizing larger digits to minimize the overall number of digits and ensure a smaller result.
Here's how the algorithm would work step-by-step:
def find_smallest_number_with_product(product):
if product == 0:
return 10
if product == 1:
return 1
digit_factors = []
# Iterate through digits 9 to 2 to find factors.
for digit in range(9, 1, -1):
while product % digit == 0:
product //= digit
digit_factors.append(digit)
# If product is not 1, no solution exists
if product != 1:
return -1
digit_factors.sort()
# Construct smallest number from sorted digits.
result = 0
for digit in digit_factors:
result = result * 10 + digit
return result| Case | How to Handle |
|---|---|
| Product is 0 | Return 10 as the smallest positive integer whose digits multiply to 0 is 10. |
| Product is 1 | Return 1, because 1 is the smallest positive integer whose digits multiply to 1. |
| Product is a prime number greater than 9 | Return -1 because no combination of single digits can multiply to a prime number greater than 9. |
| Product is a very large number that might result in integer overflow when constructing the final number | The algorithm should avoid directly constructing the final number as an integer until the digits are determined to avoid potential overflow; instead, store digits in a list or string then sort and convert if needed. |
| Product is a number with only one factor > 9, which makes a combination of single-digit factors impossible | Return -1 if after repeated division by factors 9 to 2, the remaining product is still > 9. |
| Product is a perfect square like 4,9,16,25,36,49,64,81 | Handle these cases appropriately by repeatedly dividing until only single digit factors remain. |
| Product contains only 2's and 3's as factors (e.g., 2*2*3*3). | The algorithm should ensure that the digits are arranged in ascending order for the smallest possible number. |
| Product is a negative number | Return -1 because the problem statement asks for a positive integer product. |