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
๐Ÿญ Digital Display FactoryDigit MachineBROKENAssemblyLine(BFS Queue)QualityChecknum > k?FinalProductReturn!d1d2d1d1d1d2d2d1d2d2Failed QCโœ“๐ŸŽฏ Key Insight: Build only valid numbers instead of checking all numbers!Conveyor belt represents BFS queue - systematic and efficient!
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.

n
2n
โœ“ Linear Growth
Space Complexity
O(2^d)

Queue can hold at most 2^d numbers for d-digit combinations

n
2n
โœ“ 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
Asked in
Google 28 Amazon 22 Microsoft 18 Meta 15
31.7K Views
Medium Frequency
~25 min Avg. Time
1.3K 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