Smallest Divisible Digit Product I - Problem

You are given two integers n and t.

Return 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, we need to find the smallest number ≥ 15 where the product of digits is divisible by 3. The number 16 has digit product 1×6 = 6, which is divisible by 3, so the answer is 16.

Input & Output

Example 1 — Basic Case
$ Input: n = 15, t = 3
Output: 16
💡 Note: Starting from 15: digit product = 1×5 = 5, not divisible by 3. Next number 16: digit product = 1×6 = 6, which is divisible by 3, so return 16.
Example 2 — Zero Digit
$ Input: n = 19, t = 7
Output: 20
💡 Note: Starting from 19: digit product = 1×9 = 9, not divisible by 7. Next number 20: digit product = 2×0 = 0, which is divisible by 7, so return 20.
Example 3 — Answer is n itself
$ Input: n = 12, t = 2
Output: 12
💡 Note: Starting from 12: digit product = 1×2 = 2, which is divisible by 2, so return 12 itself.

Constraints

  • 1 ≤ n ≤ 106
  • 1 ≤ t ≤ 106

Visualization

Tap to expand
Find Smallest Number ≥ 15 with Digit Product Divisible by 3Inputn = 15t = 3151 × 5 = 55 % 3 ≠ 0161 × 6 = 66 % 3 = 0 ✓Output16First valid number+1Check each number starting from n until digit product is divisible by t
Understanding the Visualization
1
Input
Given n=15, t=3, find smallest number ≥ 15
2
Process
Check digit products: 15→5, 16→6
3
Output
Return 16 (first with digit product divisible by 3)
Key Takeaway
🎯 Key Insight: Iterate from n upwards checking digit products until finding one divisible by t
Asked in
Google 25 Microsoft 15
12.0K Views
Medium Frequency
~15 min Avg. Time
450 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