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.
An integer divisible by the sum of its digits is said to be a Harshad number. You are given an integer x. Return the sum of the digits of x if x is a Harshad number, otherwise, return -1.
Example 1:
Input: x = 18
Output: 9
Explanation:
The sum of digits of x is 9. 18 is divisible by 9. So 18 is a Harshad number and the answer is 9.
Example 2:
Input: x = 23
Output: -1
Explanation:
The sum of digits of x is 5. 23 is not divisible by 5. So 23 is not a Harshad number and the answer is -1.
Constraints:
1 <= x <= 100Problem summary: An integer divisible by the sum of its digits is said to be a Harshad number. You are given an integer x. Return the sum of the digits of x if x is a Harshad number, otherwise, return -1.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
18
23
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3099: Harshad Number
class Solution {
public int sumOfTheDigitsOfHarshadNumber(int x) {
int s = 0;
for (int y = x; y > 0; y /= 10) {
s += y % 10;
}
return x % s == 0 ? s : -1;
}
}
// Accepted solution for LeetCode #3099: Harshad Number
func sumOfTheDigitsOfHarshadNumber(x int) int {
s := 0
for y := x; y > 0; y /= 10 {
s += y % 10
}
if x%s == 0 {
return s
}
return -1
}
# Accepted solution for LeetCode #3099: Harshad Number
class Solution:
def sumOfTheDigitsOfHarshadNumber(self, x: int) -> int:
s, y = 0, x
while y:
s += y % 10
y //= 10
return s if x % s == 0 else -1
// Accepted solution for LeetCode #3099: Harshad Number
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3099: Harshad Number
// class Solution {
// public int sumOfTheDigitsOfHarshadNumber(int x) {
// int s = 0;
// for (int y = x; y > 0; y /= 10) {
// s += y % 10;
// }
// return x % s == 0 ? s : -1;
// }
// }
// Accepted solution for LeetCode #3099: Harshad Number
function sumOfTheDigitsOfHarshadNumber(x: number): number {
let s = 0;
for (let y = x; y; y = Math.floor(y / 10)) {
s += y % 10;
}
return x % s === 0 ? s : -1;
}
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.