Tap here to switch tabs
Problem
Submissions

Problem: Next Greater Element III

med
30 min
Explore how to rearrange digits of a positive integer to find the smallest number greater than it using the two pointers approach. Understand constraints like maintaining digit counts and 32-bit integer bounds. This lesson prepares you to efficiently solve similar linear data problems in coding interviews.

Statement

Given a positive integer n, rearrange its digits to form the smallest possible integer that is greater than n.

The resulting integer must use exactly the same digits as n, with each digit used the same number of times.

If it is not possible to create a larger number using the same digits, return -1.

Note: If the resulting number does not fit within a 32-bit signed integer, return -1.

Constraints:

  • 1≤1 \leq n ≤231−1\leq 2^{31} - 1

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Next Greater Element III

med
30 min
Explore how to rearrange digits of a positive integer to find the smallest number greater than it using the two pointers approach. Understand constraints like maintaining digit counts and 32-bit integer bounds. This lesson prepares you to efficiently solve similar linear data problems in coding interviews.

Statement

Given a positive integer n, rearrange its digits to form the smallest possible integer that is greater than n.

The resulting integer must use exactly the same digits as n, with each digit used the same number of times.

If it is not possible to create a larger number using the same digits, return -1.

Note: If the resulting number does not fit within a 32-bit signed integer, return -1.

Constraints:

  • 1≤1 \leq n ≤231−1\leq 2^{31} - 1