FastPrepNext Number with Disjoint Digits

Next Number with Disjoint Digits

ElevenLabs logoElevenLabs● MediumFULLTIMEOA
Learn

Problem statement

Given a positive 32-bit integer n, find the smallest integer strictly greater than n whose decimal digits do not occur anywhere in the decimal representation of n.

Repeated digits are allowed in the result. Return -1 when no qualifying integer fits in a signed 32-bit integer.

Function

nextDisjointDigitNumber(n: int) → int

Examples

Example 1

n = 654321return = 700000

Digits 0, 7, 8, and 9 are allowed. The smallest allowed six-digit number above n is 700000.

Example 2

n = 98return = 100

The result cannot contain 9 or 8; 100 is the first greater number using only allowed digits.

Constraints

  • 1 <= n < 2^31 - 1.
  • The result, when it exists, is at most 2^31 - 1.
  • Leading zeroes are not part of a decimal representation.

More ElevenLabs problems

See ElevenLabs hiring insights
public int nextDisjointDigitNumber(int n) {
    // Write your code here.
}
n654321
expected700000
Checking account…