human
Largest permutation after at most k swaps | Function to find the largest permutation after k swaps.
Storing the elements and their index in map.
If number of swaps allowed are equal to number of elements then the resulting permutation will be descending order of given permutation ..
If j is not at it 's best index.
Change the index of the element which was at position 0. Swap the element basically ..
Decrement number of swaps.
Driver Code.
K is the number of swaps.
n is the size of the array.
Function calling"
How can the above be solved in Python?
assistant
#Here is code to largest permutation after at most k swaps and the other steps required.
 def bestpermutation ( arr , k , n ) :
	h = { }
	for i in range ( n ) :
		h [ arr [ i ] ] = i
	if ( n <= k ) :
		arr . sort ( )
		arr . reverse ( )
	else :
		for j in range ( n , 0 , - 1 ) :
			if ( k > 0 ) :
				initial_index = h [ j ]
				best_index = n - j
				if ( initial_index != best_index ) :
					h [ j ] = best_index
					element = arr [ best_index ]
					h [ element ] = initial_index
					arr [ best_index ] , arr [ initial_index ] = ( arr [ initial_index ] , arr [ best_index ] )
					k -= 1
arr = [ 3 , 1 , 4 , 2 , 5 ]
k = 10
n = len ( arr )
bestpermutation ( arr , k , n )
print ( " Largest ▁ possible ▁ permutation ▁ after " , k , " swaps ▁ is " , end = " ▁ " )
for i in range ( n ) :
	print ( arr [ i ] , end = " ▁ " )