Given an array of positive integers arr (not necessarily distinct), return the lexicographically largest permutation that is smaller than arr, that can be made with exactly one swap (A swap exchanges the positions of two numbers arr[i] and arr[j]). If it cannot be done, then return the same array.

&nbsp;
Example 1:

Input: arr = [3,2,1]
Output: [3,1,2]
Explanation: Swapping 2 and 1.


Example 2:

Input: arr = [1,1,5]
Output: [1,1,5]
Explanation: This is already the smallest permutation.


Example 3:

Input: arr = [1,9,4,6,7]
Output: [1,7,4,6,9]
Explanation: Swapping 9 and 7.


&nbsp;
Constraints:


	1 &lt;= arr.length &lt;= 104
	1 &lt;= arr[i] &lt;= 104


class Solution:

    def find_max(self, i, a, n):
        maxs = i+1
        for j in range(n-1, i, -1):
            # if only j is greater than max and smaller than first descending element
            if(a[maxs] <= a[j] and a[j] < a[i]):
                maxs = j
        # Swap
        a[i], a[maxs] = a[maxs], a[i]
        return a

    def prevPermOpt1(self, arr):
        n = len(arr)
        for i in range(n-1, 0, -1):
            if(arr[i] < arr[i-1]):
                # sending the first descending element from right to max_function
                arr = self.find_max(i-1, arr, n)
                break
        return arr
class Solution {
    public int[] prevPermOpt1(int[] arr) {
        int n=arr.length;
        int small=arr[n-1];
        int prev=arr[n-1];
        for(int i=n-2;i>=0;i--){
            if(arr[i]<=prev){
                prev=arr[i];
            }
            else{
                int indte=i;
                int te=0;
                    for(int j=i+1;j<n;j++){
                        if(arr[j]<arr[i]&&arr[j]>te){
                            te=arr[j];
                            indte=j;
                        }
                }
                int tem=arr[indte];
                arr[indte]=arr[i];
                arr[i]=tem;
                return arr;
            }
        }
        return arr;
    }
}
class Solution {
public:
    vector<int> prevPermOpt1(vector<int>& arr) {
        int i, p, mn = -1;
        for(i=arr.size() - 2; i>=0; i--) {
            if(arr[i] > arr[i + 1]) break;
        }
        if(i == -1) return arr;

        for(int j=i + 1; j<arr.size(); j++) {
            if(arr[j] > mn && arr[j] < arr[i]) mn = arr[j], p = j;
    
        }
        swap(arr[i], arr[p]);
        return arr;
    }
};
var prevPermOpt1 = function(arr) {
    const n = arr.length;
    let i = n - 1;
    
    while (i > 0 && arr[i] >= arr[i - 1]) i--;
    
    if (i === 0) return arr;

    const swapIndex = i - 1;
    const swapDigit = arr[swapIndex];
    
    let maxIndex = i;
    i = n - 1;
    
    while (swapIndex < i) {
        const currDigit = arr[i];
        
        if (currDigit < swapDigit && currDigit >= arr[maxIndex]) maxIndex = i;
        i--;
    }

    [arr[maxIndex], arr[swapIndex]] = [arr[swapIndex], arr[maxIndex]];
    
    return arr; 
}; 
Previous Permutation With One Swap