Check Divisibility by Digit Sum and Product - Solution & Explanation
Problem Statement
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 <= 106
Approach Overview
Problem 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.
Solution
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).
Code
Python
Java
C++
Go
TypeScript
Detailed Complexity Analysis
| 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 |
Video Solution
3622. Check Divisibility by Digit Sum and Product (Leetcode Easy) • Programming Live with Larry • 278 views views
Watch 9 more video solutions →Frequently Asked Questions
Is Check Divisibility by Digit Sum and Product easy or hard?
Check Divisibility by Digit Sum and Product Python/Java solution
How to solve Check Divisibility by Digit Sum and Product in O(n)?
What is the best approach for Check Divisibility by Digit Sum and Product?
Is Check Divisibility by Digit Sum and Product asked at Google/Amazon/Meta?
What data structure is used in Check Divisibility by Digit Sum and Product?
What is the time complexity of Check Divisibility by Digit Sum and Product?
Ready to solve this problem?
Practice Check Divisibility by Digit Sum and Product with our built-in code editor and test cases.
Practice on FleetCodeProblem Info
Table of Contents
Practice this problem
Open in Editor