Skip to main content

Check Divisibility by Digit Sum and Product - Solution & Explanation

EasyMath5 min read
Practice this problem

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

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen 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 TraversalO(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 Larry278 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Check Divisibility by Digit Sum and Product easy or hard?
The problem is classified as Easy. It mainly tests basic number manipulation, loop logic, and understanding of digit extraction using modulo and division operations.
Check Divisibility by Digit Sum and Product Python/Java solution
Both Python and Java implementations follow the same logic: repeatedly extract digits using modulo and division, compute the digit sum and digit product, then check divisibility with a final modulo operation. The algorithm runs in O(d) time and constant space in both languages.
How to solve Check Divisibility by Digit Sum and Product in O(n)?
Treat n as a sequence of digits and iterate through them once. For each digit, update the cumulative sum and product. After processing all digits, compute the divisor (digitSum + digitProduct) and perform a single modulo check with the original number.
What is the best approach for Check Divisibility by Digit Sum and Product?
The most efficient approach is digit simulation using modulo and division. Extract each digit, compute the running digit sum and digit product, then check if the original number is divisible by their combined value. This runs in O(d) time where d is the number of digits and uses O(1) extra space.
Is Check Divisibility by Digit Sum and Product asked at Google/Amazon/Meta?
Problems involving digit manipulation and arithmetic simulation appear frequently in screening rounds at large tech companies. While this exact problem may vary by platform, the underlying technique—iterating through digits and applying mathematical checks—is common in coding interviews.
What data structure is used in Check Divisibility by Digit Sum and Product?
No complex data structure is required. The solution uses simple integer variables to track the digit sum and digit product while iterating through the digits of the number.
What is the time complexity of Check Divisibility by Digit Sum and Product?
The time complexity is O(d), where d represents the number of digits in the integer. Each digit is processed exactly once while calculating the digit sum and digit product. Space complexity is O(1) when using arithmetic digit extraction.

Ready to solve this problem?

Practice Check Divisibility by Digit Sum and Product with our built-in code editor and test cases.

Practice on FleetCode