human
Generate an N | Function to calculate GCD of two integers.
Function to calculate GCD of a given array.
Utility function to check for all the possible combinations.
If an N - length sequence is obtained.
If GCD of the sequence is K.
Otherwise.
Add an element from the first array.
Recursively proceed further.
If combination satisfies the necessary condition.
Remove the element from the combination.
Add an element from the second array.
Recursive proceed further.
If combination satisfies the necessary condition.
Remove the element from the combination.
Function to check all the possible combinations.
Stores the subsequence.
If GCD is not equal to K for any combination.
Given arrays.
Given value of K.
Function call to generate the required subsequence"
How can the above be solved in Python?
assistant
#Here is code to generate an N and the other steps required.
 def GCD ( a , b ) :
	if not b :
		return a
	return GCD ( b , a % b )
def GCDArr ( a ) :
	ans = a [ 0 ]
	for i in a :
		ans = GCD ( ans , i )
	return ans
def findSubseqUtil ( a , b , ans , k , i ) :
	if len ( ans ) == len ( a ) :
		if GCDArr ( ans ) == k :
			print ( ans )
			return True
		else :
			return False
	ans . append ( a [ i ] )
	temp = findSubseqUtil ( a , b , ans , k , i + 1 )
	if temp == True :
		return True
	ans . pop ( )
	ans . append ( b [ i ] )
	temp = findSubseqUtil ( a , b , ans , k , i + 1 )
	if temp == True :
		return True
	ans . pop ( )
def findSubseq ( A , B , K , i ) :
	ans = [ ]
	findSubseqUtil ( A , B , ans , K , i )
	if not ans :
		print ( - 1 )
A = [ 5 , 3 , 6 , 2 , 9 ]
B = [ 21 , 7 , 14 , 12 , 28 ]
K = 3
ans = findSubseq ( A , B , K , 0 )