#3345

Smallest Divisible Digit Product I

Easy
MathEnumerationEnumerationBrute Force
LeetCode ↗

Approaches

Brute ForceOptimal
Complexity Comparison
Brute ForceOptimal Solution
Time
O(n²)
O(1)
Space
O(1)
O(1)
💡

Intuition

Time O(1)Space O(1)

Instead of checking every number sequentially, we can limit our checks to a maximum of 10 numbers, as the product of digits will cycle through combinations quickly.

⚙️

Algorithm

3 steps
  1. 1Step 1: Start from n and check if the product of its digits is divisible by t.
  2. 2Step 2: If not, increment n and check again, but only do this for up to 10 iterations.
  3. 3Step 3: Return the first number found that meets the criteria.
solution.py14 lines
1# Full working Python code
2
3def digit_product(x):
4    product = 1
5    while x > 0:
6        product *= x % 10
7        x //= 10
8    return product
9
10
11def smallest_divisible_digit_product(n, t):
12    for i in range(10):
13        if digit_product(n + i) % t == 0:
14            return n + i

Complexity note: The time complexity is O(1) since we only check a maximum of 10 numbers, making it constant time regardless of the input size.

  • 1The product of digits can be quickly computed, and checking divisibility is efficient.
  • 2The constraints allow us to limit our checks to a small range, making the problem manageable.

Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.