↓Skip to main content
  1. LeetCode/

LeetCode 3622: Check Divisibility by Digit Sum and Product

·1 min· ·
LeetCode Easy Math
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

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
#

  • “n divisible by x” is n % x == 0, not x % 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).

Related

LeetCode 3536: Maximum Product of Two Digits
·2 mins
LeetCode Easy Math Sorting
LeetCode 3536 note: multiply the largest and second-largest digits; when a new maximum appears, the old maximum must move to second place
LeetCode 3492: Maximum Containers on a Ship
·1 min
LeetCode Easy Math
LeetCode 3492 note: at most n² cells and at most maxWeight // w by weight, so take the smaller, using integer division
LeetCode 1103: Distribute Candies to People
·1 min
LeetCode Easy Math Simulation
LeetCode 1103 note: simulate the rounds, giving i + 1 candies on the i-th turn, and hand all the rest to the current person when short
LeetCode 1137: N-th Tribonacci Number
·2 mins
LeetCode Easy Math Dynamic-Programming Memoization
LeetCode 1137 note: like Fibonacci, roll three variables; watch the base values T0 = 0, T1 = T2 = 1 and the loop range
LeetCode 1030: Matrix Cells in Distance Order
·2 mins
LeetCode Easy Array Math Geometry Sorting
LeetCode 1030 note: list every coordinate and sort by Manhattan distance to the center; return coordinates, not distances
LeetCode 1025: Divisor Game
·2 mins
LeetCode Easy Math Dynamic-Programming Brainteaser Game-Theory
LeetCode 1025 note: dp[i] holds when some divisor x of i makes dp[i - x] a loss; observing the pattern, the answer is simply whether n is even