Watch 10 video solutions for Next Greater Numerically Balanced Number, a medium level problem involving Math, Backtracking, Enumeration. This walkthrough by codestorywithMIK has 9,354 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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 <= 106Problem Overview: Given an integer n, return the smallest integer strictly greater than n where every digit appears exactly as many times as its value. For example, 22 is numerically balanced because digit 2 appears twice, while 1333 is balanced because digit 1 appears once and digit 3 appears three times.
Approach 1: Brute Force Search (Time: O(k * d), Space: O(1))
Start from n + 1 and increment one number at a time. For each candidate, count digit frequencies using a small array of size 10. A number is numerically balanced if every digit d appears exactly d times and digits with frequency > 0 satisfy the rule. Each validation scans the digits of the number, which takes O(d) where d is the number of digits. Since the search space is small (balanced numbers are rare and typically below 107), this brute force approach performs surprisingly well in practice. This method mainly relies on simple counting logic from math and is easy to implement during interviews.
Approach 2: Permutations with Digit Limits (Time: O(P), Space: O(P))
Instead of scanning every number, generate only valid numerically balanced numbers. The key observation: a balanced number can only contain digits 1–9 where the count of each digit equals the digit itself. That means valid digit sets look like {1}, {2,2}, {1,2,2}, {3,3,3}, etc. Build candidate digit multisets where the frequency rule already holds, then generate all permutations of those digits. Convert each permutation to an integer and keep only those greater than n. Finally return the minimum valid candidate. Generation can be implemented using backtracking or direct enumeration. Because the total number of balanced numbers is tiny, this approach efficiently precomputes all possibilities and performs a simple comparison search.
Recommended for interviews: Start with brute force to show you understand the definition and validation logic. Then explain the enumeration insight: balanced numbers have strict digit-frequency constraints, so generating valid permutations dramatically reduces the search space. Interviewers usually expect you to recognize this constraint-based enumeration since it avoids unnecessary checks and demonstrates stronger algorithmic reasoning.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Search | O(k * d) | O(1) | Quick implementation when constraints are small and balanced numbers are sparse |
| Permutations with Digit Limits | O(P) | O(P) | Preferred approach when you want to generate only valid balanced numbers and avoid scanning every integer |