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 <= 1000When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach for this problem involves examining every single number from one up to your given upper limit, one by one, to see if it meets the condition. For each number, you take all of its individual digits, add them together, and check if that total sum is an even number.
Here's how the algorithm would work step-by-step:
class Solution:
def countEven(self, upper_limit: int) -> int:
valid_number_count = 0
# We iterate from one up to the upper limit to examine every single number as required
for current_number in range(1, upper_limit + 1):
digit_sum_total = 0
number_copy = current_number
# We extract individual digits to compute their cumulative sum for the parity check
while number_copy > 0:
digit_sum_total += number_copy % 10
number_copy //= 10
# We check if the digit sum is divisible by two to determine if the number is valid
if digit_sum_total % 2 == 0:
valid_number_count += 1
return valid_number_count
Instead of checking every single number one by one and adding up their digits, we can use a clever mathematical pattern to count the valid numbers almost instantly. By looking at how even and odd sums behave in groups of numbers, we can figure out the answer with a simple formula.
Here's how the algorithm would work step-by-step:
def count_even_digit_sum(target_number):
# Half the numbers in complete blocks always yield an even digit sum.
base_count = target_number // 2
digit_sum_accumulator = sum(int(digit_char) for digit_char in str(target_number))
# The final boundary number dictates whether the trailing offset is included.
has_even_digit_sum = (digit_sum_accumulator % 2 == 0)
return base_count + (1 if (target_number % 2 != 0 and has_even_digit_sum) else 0)| Case | How to Handle |
|---|---|
| Minimum possible input where num is equal to 1 | The algorithm correctly calculates the digit sum of 1 as 1, recognizes it is odd, and returns a count of 0. |
| Input num is a single-digit even number such as 2 | It evaluates integers 1 and 2, finds that only 2 has an even digit sum, and returns a count of 1. |
| Input num is a power of 10 like 100 | The digit sum function processes each number up to 100 independently and correctly sums the valid even counts. |
| Maximum constraint boundary value for num | Since the upper bound is typically small such as 1000 in this problem, a simple iterative approach easily scales without performance issues. |
| Numbers with trailing zeros like 20 or 200 | The modulo and division digit extraction logic accurately handles zeros without altering the core sum calculation. |
| Inputs where all digits are nine such as 99 | The digit sum accumulates correctly to 18, which is even, and counts the number as valid. |
| Zero or negative inputs violating the positive integer constraint | Problem constraints specify a positive integer, but the function can guard against this by checking if num is less than 1 and returning 0. |
| Language-specific integer overflow during digit summation | Standard 32-bit or 64-bit integers easily accommodate the small intermediate values produced by typical problem constraints. |