var maximumSwap = function(num) {
	const nums = `${num}`.split('');

	for (let index = 0; index < nums.length - 1; index++) {
		const current = nums[index];
		const diff = nums.slice(index + 1);
		const max = Math.max(...diff);

		if (current >= max) continue;
		const swapIndex = index + diff.lastIndexOf(`${max}`) + 1;
		[nums[index], nums[swapIndex]] = [nums[swapIndex], nums[index]];
		break;
	}
	return nums.join('');
};
class Solution {
    public int maximumSwap(int num) {
        char str[]=String.valueOf(num).toCharArray();
        char arr[]=str.clone();
        Arrays.sort(arr);
        int i=0;
        int j=str.length-1;
        while(i<str.length && j>=0 && arr[j]==str[i]){i++;j--;}
        int search=j;
        if(i==str.length) return num;
        j=str.length-1;
        while(arr[search]!=str[j]){j--;}

        char c=str[i];
        str[i]=str[j];
        str[j]=c;
        return Integer.parseInt(new String(str));
    }
}
Maximum Swap
class Solution:
    def maximumSwap(self, num: int) -> int:
        digits = [int(x) for x in str(num)]
        n = len(digits)
        
        for i in range(n):
            maxx = digits[i]
            indx = i
            
            for j in range(i+1,n):
                if digits[j]>=maxx and digits[j]!=digits[i]:
                    maxx = digits[j]
                    indx = j
                                        
            if indx!=i:
                digits[i],digits[indx] = digits[indx],digits[i]
                    
                #only one swap allowed
                return "".join([str(x) for x in digits])
                
        #already sorted
        return num
You are given an integer num. You can swap two digits at most once to get the maximum valued number.

Return the maximum valued number you can get.

&nbsp;
Example 1:

Input: num = 2736
Output: 7236
Explanation: Swap the number 2 and the number 7.


Example 2:

Input: num = 9973
Output: 9973
Explanation: No swap.


&nbsp;
Constraints:


	0 &lt;= num &lt;= 108


class Solution {
public:
    int maximumSwap(int num) {
        string st_n=to_string(num);
        int maxNum=-1,maxIdx=-1;
        int leftidx=-1,rightidx=-1;
        for(int i=st_n.size()-1;i>=0;i--)
        {
            if(st_n[i]>maxNum)
            {
                maxNum=st_n[i];
                maxIdx=i;
                continue;
            }

            if(st_n[i]<maxNum)
            {
                leftidx=i;
                rightidx=maxIdx;
            }
        }
        if(leftidx==-1)
            return num;

        swap(st_n[leftidx],st_n[rightidx]);
        return stoi(st_n);
    }
};