Smallest Greater Multiple Made of Two Digits - Problem
Imagine you're working with a digital display system that can only show two specific digits! Given three integers k, digit1, and digit2, your challenge is to find the smallest integer that satisfies all these conditions:
- ๐ข Larger than k - Must exceed our starting number
- ๐ฏ Multiple of k - Must be divisible by k with no remainder
- ๐ Only uses digit1 and/or digit2 - Like a broken calculator that only has two working digit buttons!
Return the smallest such integer, or -1 if no valid number exists or if the result exceeds 231 - 1.
Example: If k=2, digit1=1, digit2=2, we can form numbers like 1, 2, 11, 12, 21, 22, 111, 112, etc. We need the smallest one > 2 that's divisible by 2, which is 12!
Input & Output
example_1.py โ Basic Case
$
Input:
k = 2, digit1 = 1, digit2 = 2
โบ
Output:
12
๐ก Note:
We need a number > 2 that uses only digits 1 and 2, and is divisible by 2. Possible numbers: 1 (too small), 2 (not greater), 11 (not divisible by 2), 12 (perfect! 12 > 2 and 12 % 2 == 0)
example_2.py โ Single Digit
$
Input:
k = 3, digit1 = 4, digit2 = 2
โบ
Output:
24
๐ก Note:
Numbers using only 2 and 4: 2, 4, 22, 24, 42, 44... We need one > 3 and divisible by 3. Check: 24 % 3 == 0 โ, so answer is 24
example_3.py โ Edge Case with 0
$
Input:
k = 7, digit1 = 1, digit2 = 0
โบ
Output:
10
๐ก Note:
Using digits 1 and 0: 1, 10, 100, 101, 110, 111, 1000... We need > 7 and divisible by 7. First candidate 10: 10 > 7 โ but 10 % 7 = 3 โ. Continue until we find a valid multiple.
Visualization
Tap to expand
Understanding the Visualization
1
Initialize Production
Start the assembly line with single digits (digit1 and digit2)
2
Quality Check
For each display: Is it bigger than prototype? Does it divide into k equal segments?
3
Expand Production
If not suitable, create larger displays by adding more digits to the end
4
First Success
Return the first display that passes all quality checks
Key Takeaway
๐ฏ Key Insight: Use BFS to generate only valid digit combinations instead of testing every possible number - like having a smart assembly line that only builds displays worth testing!
Time & Space Complexity
Time Complexity
O(2^d)
Where d is the number of digits needed. In practice, much faster than brute force due to early termination.
โ Linear Growth
Space Complexity
O(2^d)
Queue can hold at most 2^d numbers for d-digit combinations
โ Linear Space
Constraints
- 1 โค k โค 104
- 0 โค digit1 โค 9
- 0 โค digit2 โค 9
- Result must not exceed 231 - 1
- Return -1 if no valid number exists or exceeds limit
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code