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.

&nbsp;
Example 1:
Input: n = 12
Output: 21
Example 2:
Input: n = 21
Output: -1

&nbsp;
Constraints:


	1 &lt;= n &lt;= 231 - 1


class Solution:
    def nextGreaterElement(self, n):
        digits = list(str(n))
        i = len(digits) - 1
        while i-1 >= 0 and digits[i] <= digits[i-1]:
            i -= 1
            
        if i == 0: return -1
        
        j = i
        while j+1 < len(digits) and digits[j+1] > digits[i-1]:
            j += 1
        
        digits[i-1], digits[j] = digits[j], digits[i-1]
        digits[i:] = digits[i:][::-1]
        ret = int(''.join(digits))
        
        return ret if ret < 1<<31 else -1
class Solution {
    public int nextGreaterElement(int n) {
        char[] arr = (n + "").toCharArray();
        
        int i = arr.length - 1;
        while(i > 0){
            if(arr[i-1] >= arr[i]){
                i--;
            }else{
                break;
            }
        }
        if(i == 0){
            return -1;
        }
        
        int idx1 = i-1;
        
        int j = arr.length - 1;
        while(j > idx1){
            if(arr[j] > arr[idx1]){
                break;
            }
            j--;
        }
        
        //Swapping
        swap(arr,idx1,j);
        
        //sorting
        int left = idx1+1;
        int right = arr.length-1;
        while(left < right){
            swap(arr,left,right);
            left++;
            right--;
        }
        
        String result = new String(arr);
        long val = Long.parseLong(result);
        
        return (val > Integer.MAX_VALUE ? -1 : (int)val);
        
    }
    
    void swap(char[]arr,int i,int j){
        char temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}
class Solution {
public:
    int nextGreaterElement(int n) {
       vector<int>vec;
       int temp = n;
        while(n>0){
            int r = n%10;
            vec.push_back(r);
            n /= 10; 
        }
        sort(vec.begin(),vec.end());
        do{
            int num=0;
            long j=0;
            int s = vec.size()-1;
            long i = pow(10,s);
            while(i>0)
           {
            num += i*vec[j++];
            i /= 10;
           }
              if(num>temp)
                 return num;
    
        } while(next_permutation(vec.begin(),vec.end()));
       return -1;
    }
};
var nextGreaterElement = function(n) {
	const MAX_VALUE = 2 ** 31 - 1;
	const nums = `${n}`.split('');
	let findPos;

	for (let index = nums.length - 2; index >= 0; index--) {
		if (nums[index] < nums[index + 1]) {
			findPos = index;
			break;
		}
	}

	if (findPos === undefined) return -1;
	for (let index = nums.length - 1; index >= 0; index--) {
		if (nums[index] > nums[findPos]) {
			[nums[index], nums[findPos]] = [nums[findPos], nums[index]];
			break;
		}
	}

	const mantissa = nums.slice(findPos + 1).sort((a, b) => a - b).join('');
	const result = Number(nums.slice(0, findPos + 1).join('') + mantissa);
	return result > MAX_VALUE ? -1 : result;
};
Next Greater Element III