Problem Description
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 an integer is the sum of all its digits.
Key Insights
- Iterate through every integer from 1 to num.
- Compute the digit sum by repeatedly extracting the last digit and reducing the number.
- Check if the digit sum is even.
- Count numbers with even digit sums and return the count.
- Given the constraint (1 <= num <= 1000), a straightforward simulation is efficient.
Space and Time Complexity
Time Complexity: O(num * d) where d is the number of digits in each integer (constant given the constraints)
Space Complexity: O(1)
Solution
We use a simple iterative approach. For each integer from 1 to num, calculate the sum of its digits using a helper function. This helper function extracts each digit using modulus and integer division. If the resulting sum is even, we increment our counter. Finally, the counter value is returned. The data structure used is just a counter variable, and the algorithm is a basic simulation that leverages arithmetic operations.