FastPrepNext Greater Numerically Balanced Number

Next Greater Numerically Balanced Number

Google logoGoogle● MediumINTERNONSITE INTERVIEW
Learn

Problem statement

A positive integer is numerically balanced when every digit d that appears in it occurs exactly d times.

Given an integer n, return the smallest numerically balanced integer that is strictly greater than n.

Function

nextBeautifulNumber(n: int) → int

Examples

Example 1

n = 1return = 22

The digit 2 occurs exactly twice, and 22 is the smallest balanced value greater than 1.

Example 2

n = 1000return = 1333

Digit 1 appears once and digit 3 appears three times.

Example 3

n = 3000return = 3133

3133 is balanced and is the first balanced value after 3000.

Constraints

  • 0 <= n <= 1000000.

More Google problems

See Google hiring insights
public int nextBeautifulNumber(int n) {
    // Write your code here.
}
n1
expected22
Checking account…