Please use Laptop/Desktop or any other large screen for the Mock Interview Session.

Next Greater Element



YouTube Video Thumbnail
Link

Watch above sample mock interview video to see how it works.
Login and Buy Premium to Start the Interview



Next Bigger Number with Same Digits

Next Bigger Number with Same Digits

Problem Statement

Given a positive integer num within the range of 1 to 2,000,000,000, find the smallest integer greater than num that contains exactly the same digits as num. If such a number does not exist or is outside the allowed range, return -1.

Examples

Input: 123
Output: 132
    
Input: 321
Output: -1
    
Input: 59876
Output: 65789
    

Constraints

  • 1 ≤ num ≤ 2,000,000,000