Find Maximum number possible by doing at | Javascript 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 ; 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 str [ i ] with str [ j ] ; If current num is more than maximum so far ; recurse of the other k - 1 swaps ; Backtrack ; Driver code
let max ; function findMaximumNum ( str , k ) { if ( k == 0 ) return ; let n = str . length ; for ( let i = 0 ; i < n - 1 ; i ++ ) { for ( let j = i + 1 ; j < n ; j ++ ) { if ( str [ i ] < str [ j ] ) { let t = str [ i ] ; str [ i ] = str [ j ] ; str [ j ] = t ; if ( ( str ) . join ( " " ) > ( max ) ) max = ( str ) . join ( " " ) ; findMaximumNum ( str , k - 1 ) ; let c = str [ i ] ; str [ i ] = str [ j ] ; str [ j ] = c ; } } } } let str = " " ; let k = 4 ; max = str ; findMaximumNum ( str . split ( " " ) , k ) ; document . write ( max + " " ) ;