An integer x is numerically balanced if for every digit d in the number x, there are exactly d occurrences of that digit in x.
Given an integer n, return the smallest numerically balanced number strictly greater than n.
Example 1:
Input: n = 1 Output: 22 Explanation: 22 is numerically balanced since: - The digit 2 occurs 2 times. It is also the smallest numerically balanced number strictly greater than 1.
Example 2:
Input: n = 1000 Output: 1333 Explanation: 1333 is numerically balanced since: - The digit 1 occurs 1 time. - The digit 3 occurs 3 times. It is also the smallest numerically balanced number strictly greater than 1000. Note that 1022 cannot be the answer because 0 appeared more than 0 times.
Example 3:
Input: n = 3000 Output: 3133 Explanation: 3133 is numerically balanced since: - The digit 1 occurs 1 time. - The digit 3 occurs 3 times. It is also the smallest numerically balanced number strictly greater than 3000.
Constraints:
0 <= n <= 106When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The problem asks for the smallest integer strictly greater than a given number that is numerically balanced. In a numerically balanced number, every digit appears a number of times equal to its value, and the digit zero cannot appear. Given the upper bound on the input, we can sequentially check consecutive integers starting from the next number until a valid balanced number is found.
Here's how the algorithm would work step-by-step:
def next_beautiful_number(n: int) -> int:
# Define a helper function to check if a number is numerically balanced.
def is_balanced(candidate: int) -> bool:
digit_counts = {}
temp_value = candidate
while temp_value > 0:
digit = temp_value % 10
if digit == 0:
return False
digit_counts[digit] = digit_counts.get(digit, 0) + 1
temp_value //= 10
# Verify that each digit appears exactly as many times as its numerical value.
for digit, count in digit_counts.items():
if digit != count:
return False
return True
current_number = n + 1
# Increment the number sequentially until a balanced number is encountered.
while not is_balanced(current_number):
current_number += 1
return current_number
To find the next numerically balanced number, we can take advantage of the fact that these special numbers are surprisingly rare and have a strict upper limit. Instead of guessing randomly or searching infinitely, we can simply check every number starting from the given starting point until we find the very first one that satisfies the balance rule.
Here's how the algorithm would work step-by-step:
class Solution:
def nextBeautifulNumber(self, starting_integer: int) -> int:
current_candidate = starting_integer + 1
# We iterate sequentially because balanced numbers are sparse and bounded within a small range.
while True:
digit_frequency_map = {}
for current_digit_char in str(current_candidate):
digit_frequency_map[current_digit_char] = digit_frequency_map.get(current_digit_char, 0) + 1
is_balanced_number = True
for digit_char, frequency in digit_frequency_map.items():
if int(digit_char) != frequency:
is_balanced_number = False
break
# Halting early guarantees we return the very first valid balanced number encountered.
if is_balanced_number:
return current_candidate
current_candidate += 1
| Case | How to Handle |
|---|---|
| Input n is already a numerically balanced number | The algorithm should immediately recognize this and return n without incrementing. |
| Input n is 0 or negative | Since the problem implies positive integers and the smallest valid number is 1, our search should start at max(1, n). |
| Extremely large inputs approaching the maximum value constraint | We must ensure the search space remains small since the largest possible balanced number within standard constraints is 122444. |
| Single-digit inputs like n = 1 | The algorithm correctly evaluates 1 since the digit 1 appears exactly 1 time. |
| Inputs requiring crossing a digit-length boundary | The search will naturally check numbers of increasing lengths up to 7 digits where a balanced number is guaranteed to exist. |
| Inputs with digits whose frequencies do not match their values | A frequency counting helper function verifies each candidate number digit by digit before returning. |
| Integer overflow during brute force incrementation | Using 64-bit integer types prevents overflow since the maximum possible answer fits well within standard integer limits. |
| Performance overhead from linear scanning of every integer | Given the small upper bound of valid balanced numbers, a simple iterative check from n upwards performs efficiently within time limits. |