Smallest Divisible Digit Product I Solution in Java

Welcome to the world of coding where we solve problems that make you question your life choices! Today, we’re diving into the Smallest Divisible Digit Product I problem. Imagine you’re at a party, and everyone is trying to find the smallest number that can be divided by a certain number of digits. Sounds like a blast, right? Well, that’s exactly what this problem is about!

Problem Description

The task is to find the smallest integer greater than or equal to n such that the product of its digits is divisible by t. If you’re scratching your head, don’t worry! It’s like trying to find the smallest pizza that can satisfy your friends’ cravings for toppings. You know, the ones who want everything from pepperoni to pineapple.

So, if you have a number n and a target t, you need to find that magical number where the product of its digits can be divided by t. If you can’t find such a number, well, you might as well throw your hands up in despair and call it a day!

Code Solution


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;
  }
}

Approach

The approach here is straightforward. The code iterates through numbers starting from n and checks if the product of the digits of each number is divisible by t. If it finds such a number, it returns it. If it doesn’t find any number in the next 10 integers, it throws an exception. It’s like looking for your keys in the last place you left them—if you can’t find them, you might as well give up!

Time and Space Complexity

Complexity Type Complexity
Time Complexity O(1)
Space Complexity O(1)

Real-World Example

Let’s say you’re organizing a game night, and you need to find the smallest number of players that can be divided into teams of a certain size. If n is the number of players you have, and t is the team size, you want to find the smallest number of players that can be evenly divided into teams. This is exactly what our code is doing—finding that perfect number of players!

Similar Problems

If you enjoyed this problem, you might want to check out these similar challenges: