Problem Description
You are given positive integers low, high, and k. A number is beautiful if it meets both of the following conditions:
- The count of even digits in the number is equal to the count of odd digits.
- The number is divisible by k.
Return the number of beautiful integers in the range [low, high].
Key Insights
- A number can only be considered beautiful if it has an equal number of even and odd digits.
- The divisibility condition by k must be checked for each number in the range.
- The brute-force approach would involve checking each number individually, which may be inefficient for large ranges.
- Efficiently counting even and odd digits can help in quickly determining the beauty of a number.
Space and Time Complexity
Time Complexity: O(n * d), where n is the number of integers in the range and d is the number of digits in each integer. Space Complexity: O(1), as we are using a fixed amount of space for counters and variables.
Solution
To solve the problem, we will iterate through each integer in the range [low, high]. For each integer, we will:
- Check if it is divisible by k.
- Count the even and odd digits.
- Determine if the counts of even and odd digits are equal.
- Increase a counter if both conditions are satisfied.
The algorithm uses simple arithmetic operations and a loop to check each number, making it straightforward yet potentially time-consuming for large ranges.