Medium

Quiz

#556 Next Greater Element III

APPROACH

Given a positive integer n, find the smallest integer that uses exactly the same set of digits as n but has a greater value. Return -1 if no such integer exists, or if the answer would exceed the 32-bit signed integer range.

Example 1:

Input: n = 12
Output: 21

Example 2:

Input: n = 21
Output: -1
1 of 4
1:00

What is the optimal approach for this problem?