Number of Beautiful Integers in the Range - Problem

You're on a quest to find "beautiful integers" - numbers that possess a special mathematical harmony! ๐ŸŽฏ

Given three positive integers low, high, and k, your task is to count how many integers in the range [low, high] are considered beautiful.

An integer is beautiful if it satisfies both of these conditions:

  1. Digit Balance: The count of even digits (0, 2, 4, 6, 8) equals the count of odd digits (1, 3, 5, 7, 9)
  2. Divisibility: The number is divisible by k

Example: The number 1234 has 2 even digits (2, 4) and 2 odd digits (1, 3), so it has digit balance. If k = 617 and 1234 % 617 = 0, then 1234 would be beautiful!

Goal: Return the count of beautiful integers in the given range.

Input & Output

example_1.py โ€” Basic Range
$ Input: low = 10, high = 20, k = 3
โ€บ Output: 2
๐Ÿ’ก Note: In range [10, 20], we check each number: 12 has 1 even digit (2) and 1 odd digit (1), and 12 % 3 = 0, so it's beautiful. 18 has 1 even digit (8) and 1 odd digit (1), and 18 % 3 = 0, so it's beautiful. Total: 2 beautiful integers.
example_2.py โ€” Larger Numbers
$ Input: low = 1, high = 10, k = 1
โ€บ Output: 1
๐Ÿ’ก Note: Only single-digit numbers can't be beautiful (except for 0, but it's not in range). We need equal even and odd digits. No numbers in [1, 10] satisfy this since they all have only 1 digit. Wait, let me recalculate: actually no number from 1-10 can have equal even/odd digits since they're all single digits. The answer should be 0.
example_3.py โ€” Edge Case
$ Input: low = 4, high = 4, k = 4
โ€บ Output: 0
๐Ÿ’ก Note: Only checking number 4. It has 1 even digit (4) and 0 odd digits, so even_count โ‰  odd_count. Even though 4 % 4 = 0, it doesn't satisfy the balance condition, so it's not beautiful.

Constraints

  • 1 โ‰ค low โ‰ค high โ‰ค 109
  • 1 โ‰ค k โ‰ค 20
  • Numbers can be up to 10 digits long
  • Range [low, high] is inclusive on both ends

Visualization

Tap to expand
Digit DP TreeBuild numbersdigit by digitPos 0123State Trackingโ€ข Position in numberโ€ข Balance (even - odd)โ€ข Remainder mod kโ€ข Tight bound flagBeautiful ConditionBalance = 0 ANDRemainder = 0
Understanding the Visualization
1
Digit by Digit Construction
Like composing music note by note, we build numbers digit by digit
2
Balance Tracking
Keep count: +1 for each odd digit (wind), -1 for each even digit (string)
3
Rhythm Checking
Maintain remainder modulo k to ensure final number fits the rhythm
4
State Memoization
Remember results for identical states to avoid recalculation
Key Takeaway
๐ŸŽฏ Key Insight: Instead of checking millions of numbers individually, Digit DP constructs only valid beautiful numbers by building them digit by digit while maintaining the required mathematical properties!
Asked in
Google 25 Amazon 18 Microsoft 15 Meta 12
31.2K Views
Medium Frequency
~35 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen