Given two positive integers num
and k
, find the smallest integer greater than or equal to k
whose digits are only num
or 0
. Return -1
if the integer does not exist.
Note:
k
is less than or equal to 9
.num
is an integer between 1
and 9
(inclusive).Example 1:
Input: num = 6, k = 20 Output: 60 Explanation: 60 is the smallest integer greater than or equal to 20 whose digits are either 6 or 0.
Example 2:
Input: num = 3, k = 5 Output: 30
Example 3:
Input: num = 4, k = 4444 Output: 4444
Example 4:
Input: num = 9, k = 500 Output: 900
Example 5:
Input: num = 1, k = 12 Output: -1
Constraints:
1 <= num <= 9
1 <= k <= 109
When 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 approach to this problem is like trying every possible number made from the two given digits, until we find one that is a multiple of the target number. We start small and gradually increase the size of the number, checking each one along the way.
Here's how the algorithm would work step-by-step:
def find_smallest_multiple(divisor, digit1, digit2):
queue = [digit1, digit2]
while queue:
current_number = queue.pop(0)
# Check if the current number is a multiple of the divisor.
if current_number % divisor == 0:
return current_number
# To avoid infinite loop, check current number is within limits
if current_number > 10000:
return -1
new_number1 = current_number * 10 + digit1
# Create the next possible number by appending the digits to the queue.
queue.append(new_number1)
new_number2 = current_number * 10 + digit2
queue.append(new_number2)
return -1
The key is to generate potential multiples in increasing order and stop as soon as we find one. We can generate these multiples using a breadth-first search approach, prioritizing shorter numbers and avoiding duplicates to improve efficiency.
Here's how the algorithm would work step-by-step:
def find_smallest_multiple(divisor, digit_one, digit_two): queue = [digit_one]
visited = {digit_one}
while queue:
current_number = queue.pop(0)
# Found the smallest multiple, so return it.
if current_number % divisor == 0:
return current_number
next_number_one = current_number * 10 + digit_one
if next_number_one <= 100000 and next_number_one not in visited:
queue.append(next_number_one)
visited.add(next_number_one)
next_number_two = current_number * 10 + digit_two
# Prevent very large numbers and avoid revisiting.
if next_number_two <= 100000 and next_number_two not in visited:
queue.append(next_number_two)
visited.add(next_number_two)
return -1
Case | How to Handle |
---|---|
digit1 or digit2 are invalid (e.g., not digits, or outside the 0-9 range). | Validate the inputs digit1 and digit2, and throw an exception or return an error code if they are invalid. |
n is negative. | Treat n as its absolute value or throw an exception as the problem statement specifies n is a positive integer. |
digit1 and digit2 are the same. | The solution should still generate valid numbers composed only of that digit and handle comparisons correctly. |
n is very large, causing potential integer overflow when generating numbers. | Use a data type capable of handling larger numbers (e.g., long, BigInteger) or consider a string-based comparison for larger numbers. |
No number greater than n can be formed using digit1 and digit2 within a reasonable limit (e.g., maximum number of digits to generate). | Implement a limit on the number of digits or the number of generated numbers, and return a specific value (e.g., -1, null) to indicate no solution found within the limit. |
digit1 and digit2 are both zero. | Handle the zero case carefully; the algorithm should generate a number greater than n, and not return zero if n is greater than zero. |
n is a very long number composed entirely of the smaller of the two digits (e.g., n=11111, digit1=1, digit2=2). | The algorithm must efficiently generate and compare numbers without unnecessary recursion or string manipulation overhead. |
leading zeros in the generated number when one or both digits are 0 | Ensure that numbers like '01', '001', etc. are not added if they do not exceed n or are immediately discarded, and instead prioritize other combinations |