3345. Smallest Divisible Digit Product I LeetCode Solution

In this guide, you will get 3345. Smallest Divisible Digit Product I LeetCode Solution with the best time and space complexity. The solution to Smallest Divisible Digit Product I problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Smallest Divisible Digit Product I solution in C++
  4. Smallest Divisible Digit Product I solution in Java
  5. Smallest Divisible Digit Product I solution in Python
  6. Additional Resources
3345. Smallest Divisible Digit Product I LeetCode Solution image

Problem Statement of 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.

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 <= 100
1 <= t <= 10

Complexity Analysis

  • Time Complexity: O(10\log n) = O(1)
  • Space Complexity: O(1)

3345. Smallest Divisible Digit Product I LeetCode Solution in C++

class Solution {
 public:
  int smallestNumber(int n, int t) {
    for (int num = n; num < n + 10; ++num)
      if (getDigitProd(num) % t == 0)
        return num;
    throw;
  }

 private:
  int getDigitProd(int num) {
    int digitProd = 1;
    while (num > 0) {
      digitProd *= num % 10;
      num /= 10;
    }
    return digitProd;
  }
};
/* code provided by PROGIEZ */

3345. Smallest Divisible Digit Product I LeetCode Solution in Java

class Solution {
  public int smallestNumber(int n, int t) {
    for (int num = n; num < n + 10; ++num)
      if (getDigitProd(num) % t == 0)
        return num;
    throw new IllegalArgumentException();
  }

  private int getDigitProd(int num) {
    int digitProd = 1;
    while (num > 0) {
      digitProd *= num % 10;
      num /= 10;
    }
    return digitProd;
  }
}
// code provided by PROGIEZ

3345. Smallest Divisible Digit Product I LeetCode Solution in Python

class Solution:
  def smallestNumber(self, n: int, t: int) -> int:
    return next(num for num in range(n, n + 10)
                if self._getDigitProd(num) % t == 0)

  def _getDigitProd(self, num: int) -> int:
    digitProd = 1
    while num > 0:
      digitProd *= num % 10
      num //= 10
    return digitProd
# code by PROGIEZ

Additional Resources

See also  2424. Longest Uploaded Prefix LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.