Next Greater Numerically Balanced Number
MEDIUMDescription
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 <= 106
Approaches
Checkout 3 different approaches to solve Next Greater Numerically Balanced Number. Click on different approaches to view the approach and algorithm in detail.
Brute Force Iteration
This approach involves checking every integer starting from n + 1 one by one until a numerically balanced number is found. For each number, a helper function determines if it meets the criteria of being numerically balanced.
Algorithm
- Start a loop with a variable
num, initialized ton + 1. - In each iteration, check if
numis a numerically balanced number using a helper function. - The helper function
isNumericallyBalanced(int num)works as follows:- Create a frequency map (e.g., an array of size 10) to count the occurrences of each digit in
num. - Convert the number to a string or use modulo/division to iterate through its digits and populate the frequency map.
- Iterate through the frequency map from
d = 0to9. - If a digit
dis present in the number (i.e., its count is greater than 0), check if its count is equal tod. - If
count[d] > 0andcount[d] != dfor anyd, the number is not balanced. Note that this implicitly handles the case ford=0, ascount[0]must be0for a number to be balanced.
- Create a frequency map (e.g., an array of size 10) to count the occurrences of each digit in
- If
isNumericallyBalanced(num)returnstrue, thennumis the smallest balanced number greater thann. Returnnum. - If not, increment
numand continue the loop.
The most straightforward way to solve this problem is to iterate upwards from n + 1. For each number, we perform a check to see if it's numerically balanced. A number is numerically balanced if, for every digit d it contains, the digit d appears exactly d times. For instance, 22 is balanced because the digit 2 appears twice. 1333 is balanced because 1 appears once and 3 appears three times. An important detail is that the digit 0 cannot be in a numerically balanced number, because if it were, it would have to appear 0 times, which is a contradiction. The loop continues until the first such number is found, which is guaranteed to be the smallest one greater than n.
class Solution {
public int nextBeautifulNumber(int n) {
int num = n + 1;
while (true) {
if (isNumericallyBalanced(num)) {
return num;
}
num++;
}
}
private boolean isNumericallyBalanced(int num) {
int[] counts = new int[10];
String s = String.valueOf(num);
for (char c : s.toCharArray()) {
counts[c - '0']++;
}
// For a number to be numerically balanced, for every digit d in it,
// the count of d must be equal to d.
for (int i = 0; i < 10; i++) {
if (counts[i] > 0 && counts[i] != i) {
return false;
}
}
return true;
}
}
Complexity Analysis
Pros and Cons
- Simple to understand and implement.
- Requires minimal memory.
- Can be slow if the gap between
nand the next numerically balanced number is large.
Code Solutions
Checking out 3 solutions in different languages for Next Greater Numerically Balanced Number. Click on different languages to view the code.
class Solution {
public
int nextBeautifulNumber(int n) {
for (int x = n + 1;; ++x) {
int[] cnt = new int[10];
for (int y = x; y > 0; y /= 10) {
++cnt[y % 10];
}
boolean ok = true;
for (int y = x; y > 0; y /= 10) {
if (y % 10 != cnt[y % 10]) {
ok = false;
break;
}
}
if (ok) {
return x;
}
}
}
}
Video Solution
Watch the video walkthrough for Next Greater Numerically Balanced Number
Similar Questions
5 related questions you might find useful
Patterns:
Data Structures:
Companies:
Subscribe to Scale Engineer newsletter
Learn about System Design, Software Engineering, and interview experiences every week.
No spam, unsubscribe at any time.