2048. Next Greater Numerically Balanced Number LeetCode Solution
In this guide, you will get 2048. Next Greater Numerically Balanced Number LeetCode Solution with the best time and space complexity. The solution to Next Greater Numerically Balanced Number problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Next Greater Numerically Balanced Number solution in C++
- Next Greater Numerically Balanced Number solution in Java
- Next Greater Numerically Balanced Number solution in Python
- Additional Resources
Problem Statement of Next Greater Numerically Balanced Number
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
Complexity Analysis
- Time Complexity: O(100000\log 100000)
- Space Complexity: O(1)
2048. Next Greater Numerically Balanced Number LeetCode Solution in C++
class Solution {
public:
int nextBeautifulNumber(int n) {
while (!isBalance(++n))
;
return n;
}
private:
bool isBalance(int num) {
vector<int> count(10);
while (num > 0) {
if (num % 10 == 0)
return false;
++count[num % 10];
num /= 10;
}
for (int i = 1; i < 10; ++i)
if (count[i] && count[i] != i)
return false;
return true;
}
};
/* code provided by PROGIEZ */
2048. Next Greater Numerically Balanced Number LeetCode Solution in Java
class Solution {
public int nextBeautifulNumber(int n) {
while (!isBalance(++n))
;
return n;
}
private boolean isBalance(int num) {
int[] count = new int[10];
while (num > 0) {
if (num % 10 == 0)
return false;
++count[num % 10];
num /= 10;
}
for (int i = 1; i < 10; ++i)
if (count[i] > 0 && count[i] != i)
return false;
return true;
}
}
// code provided by PROGIEZ
2048. Next Greater Numerically Balanced Number LeetCode Solution in Python
class Solution:
def nextBeautifulNumber(self, n: int) -> int:
def isBalance(num: int) -> bool:
count = [0] * 10
while num > 0:
if num % 10 == 0:
return False
count[num % 10] += 1
num //= 10
return all(c == i for i, c in enumerate(count) if c)
n += 1
while not isBalance(n):
n += 1
return n
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.