# 2048. Next Greater Numerically Balanced Number

![](https://4272748102-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LekNH5IywF8mjBxFcnu%2Fuploads%2Fr5sucTlvqpDAgTEa4FgB%2F%E5%9C%96%E7%89%87.png?alt=media\&token=d23d4673-9f5c-4a3c-887b-b3782083a4c4)

![](https://4272748102-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LekNH5IywF8mjBxFcnu%2Fuploads%2FZhKpks9beHHicz7VIXQT%2F%E5%9C%96%E7%89%87.png?alt=media\&token=2a7a28ab-f456-495e-ae4f-80b3f8d5ef00)

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
```
