diff --git a/leetcode/2180. Count Integers With Even Digit Sum/README.md b/leetcode/2180. Count Integers With Even Digit Sum/README.md new file mode 100644 index 00000000..f6006f2c --- /dev/null +++ b/leetcode/2180. Count Integers With Even Digit Sum/README.md @@ -0,0 +1,62 @@ +# [2180. Count Integers With Even Digit Sum (Easy)](https://leetcode.com/problems/count-integers-with-even-digit-sum/) + +

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 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:

+ + + + +**Similar Questions**: +* [Sum of Digits of String After Convert (Easy)](https://leetcode.com/problems/sum-of-digits-of-string-after-convert/) + +## Solution 1. Brute Force + +```cpp +// OJ: https://leetcode.com/contest/weekly-contest-281/problems/count-integers-with-even-digit-sum/ +// Author: github.com/lzl124631x +// Time: O(NlgN) +// Space: O(1) +class Solution { +public: + int countEven(int num) { + int ans = 0; + for (int i = 1; i <= num; ++i) { + int n = i, sum = 0; + while (n) { + sum += n % 10; + n /= 10; + } + ans += sum % 2 == 0; + } + return ans; + } +}; +``` + +## Discuss + +https://leetcode.com/problems/count-integers-with-even-digit-sum/discuss/1784735 \ No newline at end of file