human
What is a code to print k different sorted permutations of a given array and the other steps required given the following:
Print k different sorted permutations of a given array | Utility function to print the original indices of the elements of the array.
Function to print the required permutations.
To keep track of original indices.
Sort the array.
Count the number of swaps that can be made.
Cannot generate 3 permutations.
Print the first permutation.
Find an index to swap and create second permutation.
Print the last permutation.
Driver code.
Function call"
Solution in Python:
assistant
#Here is code to print k different sorted permutations of a given array and the other steps required.
 def printIndices ( n , a ) :
	for i in range ( n ) :
		print ( a [ i ] [ 1 ] , end = " ▁ " )
	print ( " " , ▁ end = " " )
def printPermutations ( n , a , k ) :
	arr = [ [ 0 , 0 ] for i in range ( n ) ]
	for i in range ( n ) :
		arr [ i ] [ 0 ] = a [ i ]
		arr [ i ] [ 1 ] = i
	arr . sort ( reverse = False )
	count = 1
	for i in range ( 1 , n ) :
		if ( arr [ i ] [ 0 ] == arr [ i - 1 ] [ 0 ] ) :
			count += 1
	if ( count < k ) :
		print ( " - 1" , end = " " )
		return
	next_pos = 1
	for i in range ( k - 1 ) :
		printIndices ( n , arr )
		for j in range ( next_pos , n ) :
			if ( arr [ j ] [ 0 ] == arr [ j - 1 ] [ 0 ] ) :
				temp = arr [ j ]
				arr [ j ] = arr [ j - 1 ]
				arr [ j - 1 ] = temp
				next_pos = j + 1
				break
	printIndices ( n , arr )
if __name__ == ' _ _ main _ _ ' :
	a = [ 1 , 3 , 3 , 1 ]
	n = len ( a )
	k = 3
	printPermutations ( n , a , k )