Count Integers With Even Digit Sum

Easy
550.

Count Integers With Even Digit Sum

Count Integers With Even Digit Sum is an easy coding problem on math and simulation, asked in Amazon, TCS and Mindtree interviews.

Given a positive integer num, return the count of integers in the range [1, num] whose digit sum is even.

Example 1

Input
num = 4
Output
2
Explanation
2 and 4 have even digit sums; 1 and 3 do not.

Example 2

Input
num = 30
Output
14
Explanation
The qualifying numbers are 2, 4, 6, 8, 11, 13, 15, 17, 19, 20, 22, 24, 26 and 28.

Example 3

Input
num = 1
Output
0

Constraints

  • 1 <= num <= 1000
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {number} num
 * @return {number}
 */
var countEven = function(num) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Count Integers With Even Digit Sum — Problem & Solution

Count Integers With Even Digit Sum solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem.

Count Integers With Even Digit Sum is an easy coding problem on math and simulation, asked in Amazon, TCS and Mindtree interviews.

  • Difficulty: Easy
  • Topics: Math, Simulation
  • Asked at: Amazon, TCS, Mindtree
  • Time limit: 2 s
  • Memory limit: 256 MB
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby

Problem statement

Given a positive integer num, return the count of integers in the range [1, num] whose digit sum is even.

Example 1

Input: num = 4
Output: 2
Explanation: 2 and 4 have even digit sums; 1 and 3 do not.

Example 2

Input: num = 30
Output: 14
Explanation: The qualifying numbers are 2, 4, 6, 8, 11, 13, 15, 17, 19, 20, 22, 24, 26 and 28.

Example 3

Input: num = 1
Output: 0

Constraints

  • 1 <= num <= 1000

Hints for Count Integers With Even Digit Sum

  1. Hint 1

    At num <= 1000 a direct loop over the range is plenty fast.

  2. Hint 2

    Compute each number's digit sum by repeated % 10 and / 10.

  3. Hint 3

    Count the ones whose sum is even.

How to solve Count Integers With Even Digit Sum

The range is small, so the honest simulation is the right answer: compute each digit sum and count the even ones.

Approach

  1. Loop i from 1 to num.
  2. Extract i's digits with repeated modulo and division, summing as you go.
  3. Increment the counter when the sum is even.

Why it works

Every integer in the range is examined exactly once and its digit sum computed exactly, so the count is exact by construction.

Complexity

  • Time — O(num · log num)
  • Space — O(1)

Pitfalls

  • Starting the loop at 0 counts an extra number — 0 has digit sum 0, which is even.
  • A closed form exists (roughly num / 2, adjusted at the boundary), but it is easy to get wrong; the loop is safer at these limits.

Reference solution

Python

def countEven(num: int) -> int:
    count = 0
    for i in range(1, num + 1):
        s = 0
        x = i
        while x > 0:
            s += x % 10
            x //= 10
        if s % 2 == 0:
            count += 1
    return count

Java

public static int countEven(int num) {
    int count = 0;
    for (int i = 1; i <= num; i++) {
        int s = 0, x = i;
        while (x > 0) { s += x % 10; x /= 10; }
        if (s % 2 == 0) count++;
    }
    return count;
}

C++

int countEven(int num) {
    int count = 0;
    for (int i = 1; i <= num; i++) {
        int s = 0, x = i;
        while (x > 0) { s += x % 10; x /= 10; }
        if (s % 2 == 0) count++;
    }
    return count;
}

JavaScript

var countEven = function(num) {
    let count = 0;
    for (let i = 1; i <= num; i++) {
        let s = 0, x = i;
        while (x > 0) { s += x % 10; x = Math.floor(x / 10); }
        if (s % 2 === 0) count++;
    }
    return count;
};

Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 337 math problems · the whole catalogue