Next Greater Element III

Problem Given a positive 32-bit integer n, return the smallest integer that uses exactly the same digits and is strictly greater than n. If none exists within 32-bit range, return -1.

Input / Output

  • Input: int n.
  • Output: next greater permutation of n's digits, or -1.

Constraints

  • n up to 2^31 − 1; the answer must also fit in 32 bits.

Example

  • n = 12 → 21; n = 21 → -1; n = 12443322 → 13222344.
asked …
LeaderboardSalaryAccount