You are given a positive integer num
. Return the number of positive integers less than or equal to num
whose digit sums are even.
num
always a positive integer?
num
is always positive.num
:
num
?
num
can be any positive integer up to large values, but let’s focus on standard constraints where num
could be up to (10^9).def countEven(num: int) -> int:
def is_even_digit_sum(n):
return sum(int(digit) for digit in str(n)) % 2 == 0
count = 0
for i in range(1, num + 1):
if is_even_digit_sum(i):
count += 1
return count
# Example Usage
num = 30
print(countEven(num)) # Output should be the count of numbers <= 30 with even digit sums
num
.is_even_digit_sum
to determine if the number’s digit sum is even.num
and (d) is the number of digits in each number (approximately (\log_{10}(num))).
num
(O(N)).This approach is straightforward and sufficient for reasonably sized num
. For larger values closer to (10^9), further optimizations or mathematical insights may be necessary to manage efficiency.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?