Count Integers With Even Digit Sum - Problem

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.

Input & Output

Example 1 — Small Number
$ Input: num = 4
Output: 2
💡 Note: Check numbers 1-4: 1(sum=1,odd), 2(sum=2,even), 3(sum=3,odd), 4(sum=4,even). Count of even sums = 2.
Example 2 — Larger Number
$ Input: num = 30
Output: 14
💡 Note: Among numbers 1-30, exactly 14 have even digit sums (like 2,4,6,8,11,13,15,17,20,22,24,26,28,29).
Example 3 — Single Digit
$ Input: num = 9
Output: 4
💡 Note: Numbers 1-9 with even digit sums: 2,4,6,8. Total count = 4.

Constraints

  • 1 ≤ num ≤ 1000

Visualization

Tap to expand
Count Integers With Even Digit SumInput: num = 4Check numbers 1 through 4:1234sum=1sum=2sum=3sum=4oddevenoddevenNumbers with even digit sums: 2, 4Output: 2
Understanding the Visualization
1
Input
Given positive integer num = 4
2
Process
Check each number 1 to 4, calculate digit sums
3
Output
Count numbers with even digit sums
Key Takeaway
🎯 Key Insight: Count numbers where the sum of digits is divisible by 2
Asked in
Google 15 Microsoft 12
32.5K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen