LeetCode Problem Workspace
Count Integers With Even Digit Sum
Solve this Easy Math plus Simulation problem by counting numbers whose digit sums are even up to a given limit efficiently.
2
Topics
5
Code langs
3
Related
Practice Focus
Easy · Math plus Simulation
Answer-first summary
Solve this Easy Math plus Simulation problem by counting numbers whose digit sums are even up to a given limit efficiently.
Ace coding interviews with Interview AiBoxInterview AiBox guidance for Math plus Simulation
Start by iterating through all integers from 1 to num and calculate each digit sum. Increment a counter whenever the sum is even. This approach is direct, avoids edge-case mistakes, and clearly connects to the Math plus Simulation pattern emphasized in this problem.
Problem Statement
Given a positive integer num, determine how many positive integers less than or equal to num have digit sums that are even. The digit sum is the total of all digits in a number.
For example, if num = 30, count all numbers from 1 to 30 where the sum of digits is even. Return the total count as the result.
Examples
Example 1
Input: num = 4
Output: 2
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
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
Solution Approach
Iterate Through All Numbers
Loop from 1 to num, compute the digit sum for each number, and check if it is even. Increment a counter for every number that satisfies this condition.
Calculate Digit Sum Efficiently
Convert the number to a string or repeatedly divide by 10 to extract digits. Sum the digits directly instead of using unnecessary data structures.
Return the Count
After iterating through all numbers, return the counter as the total number of integers with even digit sums. This finalizes the simulation approach cleanly.
Complexity Analysis
| Metric | Value |
|---|---|
| Time | Depends on the final approach |
| Space | Depends on the final approach |
Time complexity is O(num * d) where d is the number of digits, due to computing digit sums for each number. Space complexity is O(1) since only a counter and temporary variables are used.
What Interviewers Usually Probe
- Check if the candidate immediately recognizes the Math plus Simulation pattern.
- Listen for an explanation of digit sum computation without extra arrays.
- Watch if they avoid overcomplicating the iteration with unnecessary data structures.
Common Pitfalls or Variants
Common pitfalls
- Forgetting that single-digit numbers still count if their digit sum is even.
- Using string conversion without considering efficiency in larger num ranges.
- Returning an incorrect count by checking for odd sums instead of even sums.
Follow-up variants
- Count numbers with odd digit sums instead of even.
- Return a list of all numbers with even digit sums rather than just the count.
- Handle larger num values efficiently using modulo arithmetic patterns.
FAQ
What is the main pattern for Count Integers With Even Digit Sum?
The primary pattern is Math plus Simulation: iterating through numbers and calculating digit sums to check parity.
Can this problem be solved without converting numbers to strings?
Yes, repeatedly dividing by 10 and summing remainders avoids string conversion and keeps space complexity minimal.
What is the expected time complexity?
O(num * d), where d is the number of digits in each number, because each digit must be summed once per number.
Are there common mistakes to avoid?
Yes, including counting numbers with odd sums, ignoring single-digit numbers, or miscomputing the digit sum loop.
Does GhostInterview provide example explanations?
Yes, it shows worked examples like num = 4 or num = 30 to clarify which numbers contribute to the even digit sum count.
Solution
Solution 1
#### Python3
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 ansSolution 2
#### Python3
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 ansContinue Topic
math
Practice more edge cases under the same topic.
arrow_forwardauto_awesomeContinue Pattern
Math plus Simulation
Expand the same solving frame across more problems.
arrow_forwardsignal_cellular_altSame Difficulty Track
Easy
Stay on this level to stabilize interview delivery.
arrow_forward