Smallest Divisible Digit Product I - Problem
Find the Magic Number! ๐ฏ
You are given two integers
For example, if
Think of it as finding the next "lucky number" where all digits multiply to create a multiple of your target value.
You are given two integers
n and t. Your task is to find the smallest number greater than or equal to n such that the product of its digits is divisible by t.For example, if
n = 15 and t = 3, you need to find the smallest number โฅ 15 where multiplying all its digits gives a result divisible by 3. The number 16 has digit product 1ร6 = 6, which is divisible by 3!Think of it as finding the next "lucky number" where all digits multiply to create a multiple of your target value.
Input & Output
example_1.py โ Simple Case
$
Input:
n = 15, t = 3
โบ
Output:
16
๐ก Note:
Starting from 15: digit product of 15 is 1ร5=5 (not divisible by 3). Next number 16 has digit product 1ร6=6, which is divisible by 3.
example_2.py โ Already Valid
$
Input:
n = 23, t = 6
โบ
Output:
23
๐ก Note:
The number 23 itself has digit product 2ร3=6, which is divisible by 6. So we return 23 directly.
example_3.py โ Zero Digit Case
$
Input:
n = 101, t = 2
โบ
Output:
111
๐ก Note:
Number 101 has digit product 1ร0ร1=0, not divisible by 2. We need to find the next number without zero digits. Numbers 102-110 all contain 0. First valid is 111 with product 1ร1ร1=1, but 1 is not divisible by 2. Next is 112 with product 1ร1ร2=2, which is divisible by 2.
Visualization
Tap to expand
Understanding the Visualization
1
Set Production Line
Start the conveyor belt at number n, our minimum required plate number
2
Quality Check Station
For each plate, multiply all digits together to get the 'magic number'
3
Divisibility Test
Check if our magic number is divisible by the lucky number t
4
Ship or Continue
If divisible, ship the plate! Otherwise, move to the next number on the line
Key Takeaway
๐ฏ Key Insight: Like a quality control system, we systematically check each candidate until we find one that meets our mathematical requirement!
Time & Space Complexity
Time Complexity
O(k ร d)
In worst case similar to brute force, but often much faster in practice
โ Linear Growth
Space Complexity
O(d)
Need to store digits of the number being constructed
โ Linear Space
Constraints
- 1 โค n โค 106
- 1 โค t โค 106
- The answer is guaranteed to exist within reasonable bounds
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code