# 2048. Next Greater Numerically Balanced Number

![](/files/71TpMGogXhx7UmCEte1p)

![](/files/zBIjDOZcQIRHtffgYxR8)

time : O(n\*logn), n is the max input,&#x20;

space: O(1)

```java
// brute force, time : O(n*logn), n is the max input, space: O(1)
class Solution {
    public int nextBeautifulNumber(int n) {
        while (true) {
            n++;
            if (isValid(n)) {
                return n;
            }
        }
    }
    private boolean isValid(int n) {
        int count[] = new int[10];
        while (n != 0) {
            if (n % 10 == 0) { // in this problem, the ans wont has 0 in each single digits, 0 cant appear 0 time
                return false;
            }
            count[n%10]++; // count digits' number
            n /= 10;
            
        }
        for (int i = 1; i < 10; i++) {
            if (count[i] != 0 && count[i] != i) { // as problem discription, digit i occurs i times
                return false;
            }
        }
        return true;
    }
}

/*
> n

start from n to find beauti number

Brute force



*/j
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://timmybeeflin.gitbook.io/cracking-leetcode/math/2048.-next-greater-numerically-balanced-number.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
