LeetCode 2180: Count Integers With Even Digit Sum Solution

Master LeetCode problem 2180 (Count Integers With Even Digit Sum), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2180. Count Integers With Even Digit Sum

Problem Explanation

Explanation

To solve this problem, we iterate through all numbers from 1 to num. For each number, we calculate the sum of its digits and check if the sum is even. If it is even, we increment a counter. Finally, we return the counter as the result.

Time complexity: O(n) Space complexity: O(1)

Solution Code

class Solution {
    public int countIntegersWithEvenDigitSum(int num) {
        int count = 0;
        for (int i = 1; i <= num; i++) {
            int digitSum = 0;
            int n = i;
            while (n > 0) {
                digitSum += n % 10;
                n /= 10;
            }
            if (digitSum % 2 == 0) {
                count++;
            }
        }
        return count;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2180 (Count Integers With Even Digit Sum)?

This page provides optimized solutions for LeetCode problem 2180 (Count Integers With Even Digit Sum) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2180 (Count Integers With Even Digit Sum)?

The time complexity for LeetCode 2180 (Count Integers With Even Digit Sum) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2180 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2180 in Java, C++, or Python.

Back to LeetCode Solutions