Skip to main content

Count Integers With Even Digit Sum

Problem Description​

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​

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

Solution Approach​

Intuition:​

To efficiently Count Integers With Even Digit Sum

Solution Implementation​

Code In Different Languages:​

Written by @Ishitamukherjee2004
 
class Solution {
countEven(num) {
let cnt = 0;
for(let i = 1; i <= num; i++){
let sum = 0;
if(i < 10){
if(i % 2 == 0) cnt++;
} else{
let temp = i;
while(temp > 0){
let rev = temp % 10;
sum += rev;
temp = Math.floor(temp / 10);
}
if(sum % 2 == 0) cnt++;
}
}
return cnt;
}
}


Complexity Analysis​

  • Time Complexity: O(n)O(n)
  • Space Complexity: O(1)O(1)
  • The time complexity is O(n)O(n),where n is the input number num. This is because the algorithm uses a single loop that iterates from 1 to num.
  • The space complexity is O(1)O(1) because we are not using any extra space.