[LeetCode] Smallest Divisible Digit Product I

3345. Smallest Divisible Digit Product I

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
bool ok(int n, int t) {
int p = 1;
while(n) {
p *= n % 10; n /= 10;
}
return p % t == 0;
}
public:
int smallestNumber(int n, int t) {
for(;;n++) if(ok(n,t)) return n;
return -1;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2024/11/10/PS/LeetCode/smallest-divisible-digit-product-i/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.