Skip to content

Practice Next Greater Element III

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#556
LeetCode Problem

Next Greater Element III

Problem Statement

Given a positive integer n, find the smallest integer which has exactly the same digits existing in the integer n and is greater in value than n. If no such positive integer exists, return -1. Note that the returned integer should fit in 32-bit integer, if there is a valid answer but it does not fit in 32-bit integer, return -1.

Source: Next Greater Element III on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
n = 12
Output
21

# Example 2

Input
n = 21
Output
-1

Constraints

1 <= n <= 2^31 - 1

Related LeetCode Problems

Practice more problems similar to Next Greater Element III.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.