human
Give me a code to minimize elements to be added to a given array such that it contains another given array as its subsequence and the other steps required given the following:
Minimize elements to be added to a given array such that it contains another given array as its subsequence | Set 2 | Function to return minimum element to be added in array B so that array A become subsequence of array B.
Stores indices of the array elements.
Iterate over the array.
Store the indices of the array elements.
Stores the LIS.
Check if element B [ i ] is in array A [ ].
Perform Binary Search.
Find the value of mid m.
Update l and r.
If found better element ' e ' for pos r + 1.
Otherwise , extend the current subsequence.
Return the answer.
Driver Code.
Given arrays.
Function call"
How can the above be solved in Python?
assistant
#Here is code to minimize elements to be added to a given array such that it contains another given array as its subsequence and the other steps required.
 def minElements ( A , B , N , M ) :
	map = { }
	for i in range ( len ( A ) ) :
		map [ A [ i ] ] = i
	subseq = [ ]
	l = 0
	r = - 1
	for i in range ( M ) :
		if B [ i ] in map :
			e = map [ B [ i ] ]
			while ( l <= r ) :
				m = l + ( r - l ) // 2
				if ( subseq [ m ] < e ) :
					l = m + 1
				else :
					r = m - 1
			if ( r + 1 < len ( subseq ) ) :
				subseq [ r + 1 ] = e
			else :
				subseq . append ( e )
			l = 0
			r = len ( subseq ) - 1
	return N - len ( subseq )
if __name__ == ' _ _ main _ _ ' :
	A = [ 1 , 2 , 3 , 4 , 5 ]
	B = [ 2 , 5 , 6 , 4 , 9 , 12 ]
	M = len ( A )
	N = len ( B )
	print ( minElements ( A , B , M , N ) )