LeetCode Problem Workspace

Check Divisibility by Digit Sum and Product

Determine if a positive integer is divisible by the sum of its digits plus their product using a math-driven strategy.

category

1

Topics

code_blocks

5

Code langs

hub

3

Related

Practice Focus

Easy · Math-driven solution strategy

bolt

Answer-first summary

Determine if a positive integer is divisible by the sum of its digits plus their product using a math-driven strategy.

Interview AiBox logo

Ace coding interviews with Interview AiBox guidance for Math-driven solution strategy

Try AiBox Copilotarrow_forward

Compute the sum and product of all digits in n, then check if n modulo their sum is zero. This approach quickly identifies divisibility without loops over ranges. Handling digits individually ensures accuracy even for larger values up to 10^6.

Problem Statement

Given a positive integer n, determine whether it is divisible by the sum of the sum of its digits and the product of its digits. For example, if n = 99, the digits sum is 18 and the product is 81, so 18 + 81 = 99, and n is divisible by this total.

Return true if n is divisible by this combined value; otherwise, return false. Constraints: 1 <= n <= 10^6. Example 1: Input: n = 23 Output: false Explanation: The sum of digits is 5, product is 6, sum+product=11, 23 % 11 != 0, so return false.

Examples

Example 1

Input: n = 99

Output: true

Since 99 is divisible by the sum (9 + 9 = 18) plus product (9 * 9 = 81) of its digits (total 99), the output is true.

Example 2

Input: n = 23

Output: false

Since 23 is not divisible by the sum (2 + 3 = 5) plus product (2 * 3 = 6) of its digits (total 11), the output is false.

Constraints

  • 1 <= n <= 106

Solution Approach

Extract Digits and Compute Sum

Convert n to a string or repeatedly divide by 10 to extract digits, then accumulate their sum to use in the divisibility check.

Compute Product of Digits

While extracting digits, multiply them to get the product. Handle zeros carefully to avoid nullifying the product.

Check Divisibility by Sum Plus Product

Add the sum and product of digits, then check if n modulo this total equals zero. Return true if divisible, otherwise false.

Complexity Analysis

Metric Value
Time Depends on the final approach
Space Depends on the final approach

Time complexity is O(d) where d is the number of digits in n since each digit is processed once. Space complexity is O(1) for integer operations, or O(d) if storing digits in an array.

What Interviewers Usually Probe

  • Look for direct computation of digit sum and product without iterating over ranges.
  • Expect handling of zeros in product calculations.
  • Be prepared to explain why adding sum and product ensures divisibility correctness.

Common Pitfalls or Variants

Common pitfalls

  • Ignoring zeros in the digit product, which may incorrectly return false.
  • Mistaking sum of digits for sum plus product in the final divisibility check.
  • Using loops over all numbers up to n instead of digit-level computation.

Follow-up variants

  • Check divisibility by sum minus product of digits.
  • Determine if n is divisible by sum of squares of digits plus their product.
  • Apply the same logic to base-k numbers instead of decimal digits.

FAQ

What is the easiest way to compute divisibility in this problem?

Compute the sum and product of all digits, then check if n % (sum + product) == 0 to decide.

Does the product of digits being zero always return false?

Not necessarily; if the sum plus product still divides n evenly, it can return true.

Can this method handle large numbers up to 10^6 efficiently?

Yes, because it only processes each digit once and uses simple arithmetic operations.

Is it necessary to convert the number to a string to extract digits?

No, you can also use integer division and modulo operations to extract digits without conversion.

How does this approach fit the math-driven solution strategy?

It directly applies arithmetic rules on digits to determine divisibility, avoiding unnecessary iterations.

terminal

Solution

Solution 1: Simulation

We can iterate through each digit of the integer $n$, calculating the digit sum $s$ and digit product $p$. Finally, we check whether $n$ is divisible by $s + p$.

1
2
3
4
5
6
7
8
9
class Solution:
    def checkDivisibility(self, n: int) -> bool:
        s, p = 0, 1
        x = n
        while x:
            x, v = divmod(x, 10)
            s += v
            p *= v
        return n % (s + p) == 0
Check Divisibility by Digit Sum and Product Solution: Math-driven solution strategy | LeetCode #3622 Easy