human
Lexicographically smallest permutation number up to K having given array as a subsequence | Function to find the lexicographically smallest permutation such that the given array is a subsequence of it.
Stores the missing elements in arr in the range [ 1 , K ].
Stores if the ith element is present in arr or not.
Loop to mark all integers present in the array as visited.
Loop to insert all the integers not visited into missing.
Append INT_MAX at end in order to prevent going out of bounds.
Pointer to the current element.
Pointer to the missing element.
Stores the required permutation.
Loop to construct the permutation using greedy approach.
If missing element is smaller that the current element insert missing element.
Insert current element.
Print the required Permutation.
Driver Code.
Function Call"
How can the above be solved in Python?
assistant
#Here is code to lexicographically smallest permutation number up to K having given array as a subsequence and the other steps required.
 def findPermutation ( K , arr ) :
	missing = [ ]
	visited = [ 0 ] * ( K + 1 )
	for i in range ( 4 ) :
		visited [ arr [ i ] ] = 1
	for i in range ( 1 , K + 1 ) :
		if ( not visited [ i ] ) :
			missing . append ( i )
	INT_MAX = 2147483647
	arr . append ( INT_MAX )
	missing . append ( INT_MAX )
	p1 = 0
	p2 = 0
	ans = [ ]
	while ( len ( ans ) < K ) :
		if ( arr [ p1 ] < missing [ p2 ] ) :
			ans . append ( arr [ p1 ] )
			p1 = p1 + 1
		else :
			ans . append ( missing [ p2 ] )
			p2 = p2 + 1
	for i in range ( 0 , K ) :
		print ( ans [ i ] , end = " ▁ " )
if __name__ == " _ _ main _ _ " :
	K = 7
	arr = [ 6 , 4 , 2 , 1 ]
	findPermutation ( K , arr )