LeetCode 3345: Smallest Divisible Digit Product I Solution
Master LeetCode problem 3345 (Smallest Divisible Digit Product I), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
3345. Smallest Divisible Digit Product I
Problem Explanation
Explanation:
To solve this problem, we will iterate starting from the given number n and check each number greater than or equal to n. For each number, we will calculate the product of its digits and check if it is divisible by t. Once we find a number that satisfies the condition, we will return it as the smallest number.
Algorithm:
- Start iterating from
n. - For each number, calculate the product of its digits.
- If the product is divisible by
t, return the current number as the smallest number. - Otherwise, continue iterating until we find a number that satisfies the condition.
Time Complexity:
The time complexity of this solution is O(d * log(n)), where d is the number of digits in n.
Space Complexity:
The space complexity of this solution is O(1).
:
Solution Code
class Solution {
public int getSmallestDivisible(int n, int t) {
for (int i = n; i <= 100; i++) {
int num = i;
int product = 1;
while (num > 0) {
int digit = num % 10;
product *= digit;
num /= 10;
}
if (product % t == 0) {
return i;
}
}
return -1; // If no valid number found
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 3345 (Smallest Divisible Digit Product I)?
This page provides optimized solutions for LeetCode problem 3345 (Smallest Divisible Digit Product I) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 3345 (Smallest Divisible Digit Product I)?
The time complexity for LeetCode 3345 (Smallest Divisible Digit Product I) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 3345 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 3345 in Java, C++, or Python.