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.
Given a positive integer num, return the number of positive integers less than or equal to num whose digit sums are even.
The digit sum of a positive integer is the sum of all its digits.
Example 1:
Input: num = 4 Output: 2 Explanation: The only integers less than or equal to 4 whose digit sums are even are 2 and 4.
Example 2:
Input: num = 30 Output: 14 Explanation: The 14 integers less than or equal to 30 whose digit sums are even are 2, 4, 6, 8, 11, 13, 15, 17, 19, 20, 22, 24, 26, and 28.
Constraints:
1 <= num <= 1000Problem summary: Given a positive integer num, return the number of positive integers less than or equal to num whose digit sums are even. The digit sum of a positive integer is the sum of all its digits.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
4
30
sum-of-numbers-with-units-digit-k)sum-of-digits-of-string-after-convert)number-of-ways-to-buy-pens-and-pencils)separate-the-digits-in-an-array)find-if-digit-game-can-be-won)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2180: Count Integers With Even Digit Sum
class Solution {
public int countEven(int num) {
int ans = 0;
for (int i = 1; i <= num; ++i) {
int s = 0;
for (int x = i; x > 0; x /= 10) {
s += x % 10;
}
if (s % 2 == 0) {
++ans;
}
}
return ans;
}
}
// Accepted solution for LeetCode #2180: Count Integers With Even Digit Sum
func countEven(num int) (ans int) {
for i := 1; i <= num; i++ {
s := 0
for x := i; x > 0; x /= 10 {
s += x % 10
}
if s%2 == 0 {
ans++
}
}
return
}
# Accepted solution for LeetCode #2180: Count Integers With Even Digit Sum
class Solution:
def countEven(self, num: int) -> int:
ans = 0
for x in range(1, num + 1):
s = 0
while x:
s += x % 10
x //= 10
ans += s % 2 == 0
return ans
// Accepted solution for LeetCode #2180: Count Integers With Even Digit Sum
/**
* [2180] Count Integers With Even Digit Sum
*
* Given a positive integer num, return the number of positive integers less than or equal to num whose digit sums are even.
* The digit sum of a positive integer is the sum of all its digits.
*
* Example 1:
*
* Input: num = 4
* Output: 2
* Explanation:
* The only integers less than or equal to 4 whose digit sums are even are 2 and 4.
*
* Example 2:
*
* Input: num = 30
* Output: 14
* Explanation:
* The 14 integers less than or equal to 30 whose digit sums are even are
* 2, 4, 6, 8, 11, 13, 15, 17, 19, 20, 22, 24, 26, and 28.
*
*
* Constraints:
*
* 1 <= num <= 1000
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/count-integers-with-even-digit-sum/
// discuss: https://leetcode.com/problems/count-integers-with-even-digit-sum/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn count_even(num: i32) -> i32 {
(2..=num)
.filter(|i| {
let (mut sum, mut x) = (0, *i);
while x > 0 {
sum += x % 10;
x /= 10;
}
(sum & 1).eq(&0)
})
.count() as _
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_2180_example_1() {
let num = 4;
let result = 2;
assert_eq!(Solution::count_even(num), result);
}
#[test]
fn test_2180_example_2() {
let num = 30;
let result = 14;
assert_eq!(Solution::count_even(num), result);
}
}
// Accepted solution for LeetCode #2180: Count Integers With Even Digit Sum
function countEven(num: number): number {
let ans = 0;
for (let i = 1; i <= num; ++i) {
let s = 0;
for (let x = i; x; x = Math.floor(x / 10)) {
s += x % 10;
}
if (s % 2 == 0) {
++ans;
}
}
return ans;
}
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.