275. Number of Beautiful Integers in the Range

You are given three positive integers low, high, and k.

An integer is considered beautiful if it satisfies both of the following conditions:

  1. The total count of even digits in the number is strictly equal to the total count of odd digits.
  2. The number is completely divisible by k.

Determine and return the total number of beautiful integers that lie strictly within the inclusive range [low, high].

Example 1:

Input: low = 10, high = 30, k = 5

Output: 3

Explanation:

The numbers divisible by 5 in this range are 10, 15, 20, 25, and 30.

  • 10 is beautiful (1 odd digit '1', 1 even digit '0').
  • 15 is not beautiful (2 odd digits).
  • 20 is not beautiful (2 even digits).
  • 25 is beautiful (1 even digit '2', 1 odd digit '5').
  • 30 is beautiful (1 odd digit '3', 1 even digit '0'). Total beautiful integers = 3.

Example 2:

Input: low = 1, high = 9, k = 2

Output: 0

Explanation:

All numbers in this range have a length of 1. A number with an odd length of digits can never have an equal count of even and odd digits. Thus, there are 0 beautiful integers.

Now Your Turn!

Pick the correct output for the given input

Input: low = 10, high = 15, k = 2

(Determine the total number of beautiful integers in the given range)

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 0 < low <= high <= 109
  • 0 < k <=20

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
int numberOfBeautifulIntegers(int low, int high, int k) {
}
};
Test Case

Input:

High
K
Low