You are given a positive integer n. Determine whether n is divisible by the sum of the following two values:
The digit sum of n (the sum of its digits).
The digit product of n (the product of its digits).
Return true if n is divisible by this sum; otherwise, return false.
Example 1:
Input: n = 99
Output: true
Explanation:
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
Explanation:
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 <= 106Problem Overview: Given an integer n, compute the sum and product of its digits. The task is to determine whether n is divisible by the value formed from those digits (commonly digitSum + digitProduct). The problem focuses on basic number manipulation using simple math operations.
Approach 1: Digit Simulation (O(d) time, O(1) space)
Iterate through every digit of the number and accumulate two values: the sum of digits and the product of digits. You can extract digits using repeated modulo and division operations (digit = n % 10, then n //= 10). After processing all digits, compute the final divisor (typically digitSum + digitProduct) and check whether the original number is divisible by it using a modulo operation.
This approach relies purely on arithmetic digit extraction, which avoids extra memory allocations. The runtime depends on the number of digits d, giving O(d) time complexity and constant O(1) space. This pattern appears frequently in math and simulation problems where you repeatedly process digits of an integer.
Approach 2: String-Based Digit Traversal (O(d) time, O(d) space)
Convert the integer to a string and iterate through each character. Convert each character back to an integer digit, update the running digit sum and digit product, and compute the same divisibility condition at the end. The logic is straightforward and often easier to read in high-level languages.
This version still runs in O(d) time because every digit is processed once. However, converting the number to a string requires O(d) extra space. Many developers prefer this version during interviews when readability matters more than strict memory usage.
Recommended for interviews: The arithmetic simulation approach. It demonstrates comfort with digit manipulation and avoids unnecessary allocations. Showing both methods helps: the string version proves clarity of thought, while the arithmetic method shows deeper control over numeric operations and constant-space optimization.
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.
The time complexity is O(log n), where n is the value of the integer n. The space complexity is O(1).
Python
Java
C++
Go
TypeScript
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Digit Simulation (mod/div extraction) | O(d) | O(1) | Best general solution; avoids extra memory and shows strong number manipulation skills |
| String Conversion Traversal | O(d) | O(d) | Useful when prioritizing readability or rapid implementation in high-level languages |
3622. Check Divisibility by Digit Sum and Product (Leetcode Easy) • Programming Live with Larry • 271 views views
Watch 9 more video solutions →Practice Check Divisibility by Digit Sum and Product with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor