FastPrepNext Greater Element III
Problems/Next Greater Element III

Next Greater Element III

The Walt Disney Company logoThe Walt Disney Company● MediumFULLTIMEOA
QuestionFunctionExamplesConstraints

Problem statement

Source note: The historical collection names LC556 / Next Greater Element III but also questions a reversed number reference, 655.

This version follows the explicit LC556 title. Decimal digit reuse, no leading zero, signed32-bit bounds and -1 for no valid result are the disclosed practice contract; the original reversal remark remains unresolved as source wording.

The judged task matches the selected reported identity at about 75%.

Given a positive integer n, rearrange exactly its decimal digits to obtain the smallest integer strictly greater than n. Return that value if it fits a signed 32-bit positive integer; otherwise return -1.

Use every digit exactly as many times as it occurs in n. Do not introduce a leading zero. This practice interpretation follows the named LC556 task in the collected report; the report also questions a reversed number reference.

Function

nextGreaterElement(n: int) → int

Examples

Example 1

n = 1243return = 1324

The rightmost increase is at digit 2. Swap it with 3, then arrange the remaining digits in increasing order: 1324. Every arrangement with the original prefix 12 is at most 1243, so none lies strictly between 1243 and 1324.

Example 2

n = 31return = -1

The digits are already in descending order, so no larger rearrangement exists. Return -1.

Constraints

  • 1 <= n <= 2147483647
  • The result must fit a signed 32-bit positive integer.

More The Walt Disney Company problems

  • Find All Paths for a Name in a File SystemPHONE SCREEN · Seen Nov 2026
  • Resizable Array-Backed Integer SetPHONE SCREEN · Seen Sep 2026
  • Shortest Path with Directional Move CostsPHONE SCREEN · Seen Sep 2026
All The Walt Disney Company interview questionsSee The Walt Disney Company hiring insights
Saved
class Solution {
    public int nextGreaterElement(int n) {
        // Write your code here
    }
}
Checking account…