Meta Data#
Difficulty: easy First Attempt: 2025-07-20
- Submissions: Wrong Answer → Accepted
Approach#
n must be divisible by “digit sum + digit product”, i.e. n % (sum + product) == 0. The digit sum is at least 1, so the divisor is never 0.
My Attempts#
Attempt 1 wrote (product + sum) % n == 0, the wrong way around. For n = 2 the sum and product are both 2, and 2 % 4 = 2, so the answer is False; it computed 4 % 2 = 0 and returned True.
Solution#
from math import prod
class Solution:
def checkDivisibility(self, n: int) -> bool:
digits = [int(c) for c in str(n)]
return n % (sum(digits) + prod(digits)) == 0
Complexity#
Time O(d), space O(d).
Mistakes to Avoid#
- “
ndivisible byx” isn % x == 0, notx % n == 0.
Interview Explanation#
I split n into digits, compute their sum and product, and check whether n modulo their total is 0. Time O(d).
