Skip to content

Commit 2f0b1fd

Browse files
committed
Added task 2180
1 parent 7372fd8 commit 2f0b1fd

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g2101_2200.s2180_count_integers_with_even_digit_sum;
2+
3+
// #Easy #Math #Simulation #2022_06_08_Time_0_ms_(100.00%)_Space_41.1_MB_(42.90%)
4+
5+
public class Solution {
6+
public int countEven(int n) {
7+
if (n % 2 == 1) {
8+
return n/2;
9+
}
10+
else {
11+
int ans = 0;
12+
int num = n;
13+
while (num != 0) {
14+
ans += num % 10;
15+
num /= 10;
16+
}
17+
if (ans % 2 == 0) {
18+
return n/2;
19+
}
20+
else {
21+
return n/2 - 1;
22+
}
23+
}
24+
}
25+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2180\. Count Integers With Even Digit Sum
2+
3+
Easy
4+
5+
Given a positive integer `num`, return _the number of positive integers **less than or equal to**_ `num` _whose digit sums are **even**_.
6+
7+
The **digit sum** of a positive integer is the sum of all its digits.
8+
9+
**Example 1:**
10+
11+
**Input:** num = 4
12+
13+
**Output:** 2
14+
15+
**Explanation:** The only integers less than or equal to 4 whose digit sums are even are 2 and 4.
16+
17+
**Example 2:**
18+
19+
**Input:** num = 30
20+
21+
**Output:** 14
22+
23+
**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.
24+
25+
**Constraints:**
26+
27+
* `1 <= num <= 1000`
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g2101_2200.s2180_count_integers_with_even_digit_sum;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void countEven() {
11+
assertThat(new Solution().countEven(4), equalTo(2));
12+
}
13+
14+
@Test
15+
void countEven2() {
16+
assertThat(new Solution().countEven(30), equalTo(14));
17+
}
18+
}

0 commit comments

Comments
 (0)