Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package g2101_2200.s2180_count_integers_with_even_digit_sum;

// #Easy #Math #Simulation #2022_06_08_Time_0_ms_(100.00%)_Space_41.1_MB_(42.90%)

public class Solution {
public int countEven(int n) {
if (n % 2 == 1) {
return n / 2;
} else {
int ans = 0;
int num = n;
while (num != 0) {
ans += num % 10;
num /= 10;
}
if (ans % 2 == 0) {
return n / 2;
} else {
return n / 2 - 1;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
2180\. Count Integers With Even Digit Sum

Easy

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

* `1 <= num <= 1000`
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package g2101_2200.s2180_count_integers_with_even_digit_sum;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void countEven() {
assertThat(new Solution().countEven(4), equalTo(2));
}

@Test
void countEven2() {
assertThat(new Solution().countEven(30), equalTo(14));
}

@Test
void countEven3() {
assertThat(new Solution().countEven(11), equalTo(5));
}
}