> For the complete documentation index, see [llms.txt](https://emmaguo100.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://emmaguo100.gitbook.io/leetcode/04-30-2022.md).

# 04/30/2022

```
// Some code
public class Solution {
    public String swap(String s, int i0, int i1) {
        if (i0 == i1)
            return s;
        String s1 = s.substring(0, i0);
        String s2 = s.substring(i0 + 1, i1);
        String s3 = s.substring(i1 + 1);
        
        return s1 + s.charAt(i1) + s2 + s.charAt(i0) + s3;
    }
    
    ArrayList < String > list = new ArrayList < > ();
    void permute(String a, int l, int r) {
        int i;
        if (l == r){
            list.add(a);
            System.out.println("just added result is " + a);
            
        }
            
        
        else {
            for (i = l; i <= r; i++) {
                System.out.println("l is " + l );
                System.out.println("i is " + i );
                
                a = swap(a, l, i);
                System.out.println("first swap is " + a);
                permute(a, l + 1, r);
                a = swap(a, l, i);
                System.out.println("second swap is " + a);
            }
        }
    }
    public int nextGreaterElement(int n) {
        String s = "" + n;
        permute(s, 0, s.length() - 1);
        Collections.sort(list);
        int i;
        for (i = list.size() - 1; i >= 0; i--) {
            if (list.get(i).equals("" + n))
                break;
        }
        return i == list.size() - 1 ? -1 : Integer.parseInt(list.get(i + 1));
    }
}
```

```
// Some code
public class Solution {
    
    public int nextGreaterElement(int n) {
        //to find the smallest integer than the current n, we need to loop from right to left
        //convert int n = to char array
        int result;
        int i, j;
        char[] arr = ("" + n).toCharArray();
        for (i = arr.length -2 ; i >= 0; i--){
            if (arr[i] < arr[i+1]) break;    
        }
        if (i < 0) return -1;
        System.out.println("value in i is " + arr[i]);
        
        for (j = arr.length -1 ; j > i; j--){
            if (arr[j] > arr[i]) { 
                break;
            }
                
        }
        swap(arr, i, j);
        //System.out.println(arr);
        Arrays.sort(arr, i+1, arr.length);
        
        //conver char array to long
        long val = Long.parseLong(new String(arr));
        return (val <= Integer.MAX_VALUE) ? (int) val : -1;
    
    }
    
    private void swap(char[] arr, int l, int r){
        char temp = arr[l];
        arr[l] = arr[r];
        arr[r] = temp;
    }
}
```

Method: Find the smallest integer which has the same exact digits in the integer n and is greater in value than n.  First ,we convert this integer to a char array. Then walk from right to the left to find the index i with a digit that is smaller than the previous one.  Then we walk from right to the index i that we just found, to find the index j with a digit greater than the digit in the index i. We swap the char value in the i and j. In order to make sure the value in the current char array is the smallest value that is greater than the original number, we need to reverse the value from index i + 1 to the end. Finally, we convert the char array to an int number. If it doesn't fit in a 32-bit integer, we return -1. else return this number.

Time O(N)

Space O(N)

```
// Some code

public class Solution {
    
    public int nextGreaterElement(int n) {
        //to find the smallest integer than the current n, we need to loop from right to left
        //convert int n = to char array
        int result;
        int i, j;
        char[] arr = ("" + n).toCharArray();
        for (i = arr.length -2 ; i >= 0; i--){
            if (arr[i] < arr[i+1]) break;    
        }
        if (i < 0) return -1;
        System.out.println("value in i is " + arr[i]);
        
        for (j = arr.length -1 ; j > i; j--){
            if (arr[j] > arr[i]) { 
                break;
            }
                
        }
        swap(arr, i, j);
        
       //reverse the char arr from the i + 1 to the end to get a smaller number
        int start = i + 1;
        int end = arr.length - 1;
        while (start < end){
            swap(arr, start, end);
            start++;
            end--;
        }
        
        //conver char array to long
       try{
           return Integer.parseInt(new String(arr));
       }catch (Exception e){
           return -1;
       }
    
    }
    
    private void swap(char[] arr, int l, int r){
        char temp = arr[l];
        arr[l] = arr[r];
        arr[r] = temp;
    }
}
```
