Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Build confidence with an intuition-first walkthrough focused on math fundamentals.
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 summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
99
23
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3622: Check Divisibility by Digit Sum and Product
class Solution {
public boolean checkDivisibility(int n) {
int s = 0, p = 1;
int x = n;
while (x != 0) {
int v = x % 10;
x /= 10;
s += v;
p *= v;
}
return n % (s + p) == 0;
}
}
// Accepted solution for LeetCode #3622: Check Divisibility by Digit Sum and Product
func checkDivisibility(n int) bool {
s, p := 0, 1
x := n
for x != 0 {
v := x % 10
x /= 10
s += v
p *= v
}
return n%(s+p) == 0
}
# Accepted solution for LeetCode #3622: Check Divisibility by Digit Sum and Product
class Solution:
def checkDivisibility(self, n: int) -> bool:
s, p = 0, 1
x = n
while x:
x, v = divmod(x, 10)
s += v
p *= v
return n % (s + p) == 0
// Accepted solution for LeetCode #3622: Check Divisibility by Digit Sum and Product
fn check_divisibility(n: i32) -> bool {
let mut m = n;
let mut sum = 0;
let mut product = 1;
while m > 0 {
let k = m % 10;
sum += k;
product *= k;
m /= 10;
}
n % (sum + product) == 0
}
fn main() {
let ret = check_divisibility(99);
println!("ret={ret}");
}
#[test]
fn test() {
assert!(check_divisibility(99));
assert!(!check_divisibility(23));
}
// Accepted solution for LeetCode #3622: Check Divisibility by Digit Sum and Product
function checkDivisibility(n: number): boolean {
let [s, p] = [0, 1];
let x = n;
while (x !== 0) {
const v = x % 10;
x = Math.floor(x / 10);
s += v;
p *= v;
}
return n % (s + p) === 0;
}
Use this to step through a reusable interview workflow for this problem.
Simulate the process step by step — multiply n times, check each number up to n, or iterate through all possibilities. Each step is O(1), but doing it n times gives O(n). No extra space needed since we just track running state.
Math problems often have a closed-form or O(log n) solution hidden behind an O(n) simulation. Modular arithmetic, fast exponentiation (repeated squaring), GCD (Euclidean algorithm), and number theory properties can dramatically reduce complexity.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.