Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #2827: Number of Beautiful Integers in the Range

In this guide, we solve Leetcode #2827 Number of Beautiful Integers in the Range in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

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.

Quick Facts

  • Difficulty: Hard
  • Premium: No
  • Tags: Math, Dynamic Programming

Intuition

The problem breaks into overlapping subproblems, so caching results prevents exponential repetition.

A carefully chosen DP state captures exactly what we need to build the final answer.

Approach

Define the DP state and recurrence, then compute states in the correct order.

Optionally compress space once the recurrence is clear.

Steps:

  • Choose a DP state definition.
  • Write the recurrence and base cases.
  • Compute states in the correct order.

Example

Input: low = 10, high = 20, k = 3 Output: 2 Explanation: There are 2 beautiful integers in the given range: [12,18]. - 12 is beautiful because it contains 1 odd digit and 1 even digit, and is divisible by k = 3. - 18 is beautiful because it contains 1 odd digit and 1 even digit, and is divisible by k = 3. Additionally we can see that: - 16 is not beautiful because it is not divisible by k = 3. - 15 is not beautiful because it does not contain equal counts even and odd digits. It can be shown that there are only 2 beautiful integers in the given range.

Python Solution

class Solution: def numberOfBeautifulIntegers(self, low: int, high: int, k: int) -> int: @cache def dfs(pos: int, mod: int, diff: int, lead: int, limit: int) -> int: if pos >= len(s): return mod == 0 and diff == 10 up = int(s[pos]) if limit else 9 ans = 0 for i in range(up + 1): if i == 0 and lead: ans += dfs(pos + 1, mod, diff, 1, limit and i == up) else: nxt = diff + (1 if i % 2 == 1 else -1) ans += dfs(pos + 1, (mod * 10 + i) % k, nxt, 0, limit and i == up) return ans s = str(high) a = dfs(0, 0, 10, 1, 1) dfs.cache_clear() s = str(low - 1) b = dfs(0, 0, 10, 1, 1) return a - b

Complexity

The time complexity is O((log⁡M)2×k×∣Σ∣)O((\log M)^2 \times k \times |\Sigma|)O((logM)2×k×∣Σ∣), and the space complexity is O((log⁡M)2×k)O((\log M)^2 \times k)O((logM)2×k), where MMM represents the size of the number highhighhigh, and ∣Σ∣|\Sigma|∣Σ∣ represents the digit set. The space complexity is O((log⁡M)2×k)O((\log M)^2 \times k)O((logM)2×k), where MMM represents the size of the number highhighhigh, and ∣Σ∣|\Sigma|∣Σ∣ represents the digit set.

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy