LeetCode Challenge Day 39 β 2048. Next Greater Numerically Balanced Number
Nitin Ahirwal / October 24, 2025
Hey folks
This is Day 39 of my LeetCode streak π.
Todayβs problem is 2048. Next Greater Numerically Balanced Number β a medium combinatorics/backtracking problem where we must find the smallest numerically balanced number strictly greater than n.
π Problem Statement
A number is numerically balanced if every digit d appears exactly d times.
For example:
22is balanced (digit2appears twice).1333is balanced (digit1appears once, digit3appears three times).
Given an integer n, return the smallest numerically balanced number strictly greater than n.
π‘ Intuition
- Only digits
1..7matter.
Any digit larger than 7 would require β₯ 8 occurrences, making the number too big (sincen β€ 1e6). - The maximum length needed is 7 digits, because the smallest 7-digit balanced number is
1224444. - So the problem can be reduced to:
Generate all numerically balanced numbers up to 7 digits, sort them, then pick the smallest one greater thann.
π Approach
-
Generate multisets
- For each digit
d β [1..7], include either0ordcopies of it. - Keep only multisets whose total length β€ 7.
- For each digit
-
Generate permutations
- For each valid multiset, generate all unique permutations to form numbers.
-
Store & sort
- Collect all numbers into a set (to remove duplicates).
- Sort them in ascending order.
-
Binary search
- Find the smallest number strictly greater than
n.
- Find the smallest number strictly greater than
β±οΈ Complexity Analysis
-
Time complexity:
The search space is bounded and small. All multisets β€ 7 digits, permutations β€7!in worst case.
Thus complexity is effectively O(1) for given constraints. -
Space complexity:
We store all generated numbers (a few thousand at most).
So, O(1) as well.
π§βπ» Code (JavaScript)
/**
* 2048. Next Greater Numerically Balanced Number
*/
var nextBeautifulNumber = function(n) {
const nums = [];
const digits = [1,2,3,4,5,6,7];
function genPermutations(arr) {
arr.sort((a,b) => a-b);
const used = new Array(arr.length).fill(false);
const cur = [], out = [];
function dfs() {
if (cur.length === arr.length) {
let val = 0;
for (let d of cur) val = val * 10 + d;
out.push(val);
return;
}
for (let i = 0; i < arr.length; i++) {
if (used[i]) continue;
if (i > 0 && arr[i] === arr[i-1] && !used[i-1]) continue;
used[i] = true;
cur.push(arr[i]);
dfs();
cur.pop();
used[i] = false;
}
}
dfs();
return out;
}
for (let mask = 1; mask < (1 << digits.length); mask++) {
let totalLen = 0;
const multiset = [];
for (let i = 0; i < digits.length; i++) {
if (mask & (1 << i)) {
const d = digits[i];
totalLen += d;
if (totalLen > 7) break;
for (let k = 0; k < d; k++) multiset.push(d);
}
}
if (multiset.length === totalLen && totalLen > 0 && totalLen <= 7) {
nums.push(...genPermutations(multiset));
}
}
const uniq = Array.from(new Set(nums)).sort((a,b) => a-b);
let lo = 0, hi = uniq.length - 1, ans = -1;
while (lo <= hi) {
const mid = (lo + hi) >> 1;
if (uniq[mid] > n) {
ans = uniq[mid];
hi = mid - 1;
} else {
lo = mid + 1;
}
}
return ans;
};
π§ͺ Example Walkthrough
Input: n = 1
Candidates include: 22, 1333, 1224444, β¦
Smallest balanced number greater than 1 is 22. β
Input: n = 300
Balanced candidates around it: 1333, 1444, 2333, β¦
The next greater is 1333. β
π₯ Reflections
This problem was fun because it looks tricky at first, but the constraints make brute-force feasible. By pre-generating all possible balanced numbers once, we can instantly answer for any n β€ 1e6.
Thatβs it for Day 39 of my LeetCode journey! Onwards to the next challenge π₯
Happy Coding π¨βπ»