Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Build confidence with an intuition-first walkthrough focused on math fundamentals.
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.
Example 1:
Input: n = 10, t = 2
Output: 10
Explanation:
The digit product of 10 is 0, which is divisible by 2, making it the smallest number greater than or equal to 10 that satisfies the condition.
Example 2:
Input: n = 15, t = 3
Output: 16
Explanation:
The digit product of 16 is 6, which is divisible by 3, making it the smallest number greater than or equal to 15 that satisfies the condition.
Constraints:
1 <= n <= 1001 <= t <= 10Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
10 2
15 3
smallest-number-with-given-digit-product)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3345: Smallest Divisible Digit Product I
class Solution {
public int smallestNumber(int n, int t) {
for (int i = n;; ++i) {
int p = 1;
for (int x = i; x > 0; x /= 10) {
p *= (x % 10);
}
if (p % t == 0) {
return i;
}
}
}
}
// Accepted solution for LeetCode #3345: Smallest Divisible Digit Product I
func smallestNumber(n int, t int) int {
for i := n; ; i++ {
p := 1
for x := i; x > 0; x /= 10 {
p *= x % 10
}
if p%t == 0 {
return i
}
}
}
# Accepted solution for LeetCode #3345: Smallest Divisible Digit Product I
class Solution:
def smallestNumber(self, n: int, t: int) -> int:
for i in count(n):
p = 1
x = i
while x:
p *= x % 10
x //= 10
if p % t == 0:
return i
// Accepted solution for LeetCode #3345: Smallest Divisible Digit Product I
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3345: Smallest Divisible Digit Product I
// class Solution {
// public int smallestNumber(int n, int t) {
// for (int i = n;; ++i) {
// int p = 1;
// for (int x = i; x > 0; x /= 10) {
// p *= (x % 10);
// }
// if (p % t == 0) {
// return i;
// }
// }
// }
// }
// Accepted solution for LeetCode #3345: Smallest Divisible Digit Product I
function smallestNumber(n: number, t: number): number {
for (let i = n; ; ++i) {
let p = 1;
for (let x = i; x; x = Math.floor(x / 10)) {
p *= x % 10;
}
if (p % t === 0) {
return i;
}
}
}
Use this to step through a reusable interview workflow for this problem.
Simulate the process step by step — multiply n times, check each number up to n, or iterate through all possibilities. Each step is O(1), but doing it n times gives O(n). No extra space needed since we just track running state.
Math problems often have a closed-form or O(log n) solution hidden behind an O(n) simulation. Modular arithmetic, fast exponentiation (repeated squaring), GCD (Euclidean algorithm), and number theory properties can dramatically reduce complexity.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.