Find Maximum number possible by doing at | Python3 program to find maximum integer possible by doing at - most K swap operations on its digits . ; function to find maximum integer possible by doing at - most K swap operations on its digits ; return if no swaps left ; consider every digit ; and compare it with all digits after it ; if digit at position i is less than digit at position j , swap it and check for maximum number so far and recurse for remaining swaps ; swap string [ i ] with string [ j ] ; If current num is more than maximum so far ; recurse of the other k - 1 swaps ; backtrack ; Driver Code
def swap ( string , i , j ) : NEW_LINE INDENT return ( string [ : i ] + string [ j ] + string [ i + 1 : j ] + string [ i ] + string [ j + 1 : ] ) NEW_LINE DEDENT def findMaximumNum ( string , k , maxm ) : NEW_LINE INDENT if k == 0 : NEW_LINE INDENT return NEW_LINE DEDENT n = len ( string ) NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if string [ i ] < string [ j ] : NEW_LINE INDENT string = swap ( string , i , j ) NEW_LINE if string > maxm [ 0 ] : NEW_LINE INDENT maxm [ 0 ] = string NEW_LINE DEDENT findMaximumNum ( string , k - 1 , maxm ) NEW_LINE string = swap ( string , i , j ) NEW_LINE DEDENT DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = "129814999" NEW_LINE k = 4 NEW_LINE maxm = [ string ] NEW_LINE findMaximumNum ( string , k , maxm ) NEW_LINE print ( maxm [ 0 ] ) NEW_LINE DEDENT