human
Generate an N | Function to calculate GCD of given array.
Function to calculate GCD of two integers.
Utility function to check for all the combinations.
If a sequence of size N is obtained.
If gcd of current combination is K.
If current element from first array is divisible by K.
Recursively proceed further.
If current combination satisfies given condition.
Remove the element from the combination.
If current element from second array is divisible by K.
Recursively proceed further.
If current combination satisfies given condition.
Remove the element from the combination.
Function to check for all possible combinations.
Stores the subsequence.
If GCD of any sequence is not equal to K.
Given arrays.
Given value of K.
Function call to generate a subsequence whose GCD is K"
Solution in Python:
assistant
#Here is code to generate an N and the other steps required.
 def GCDArr ( a ) :
	ans = a [ 0 ]
	for i in a :
		ans = GCD ( ans , i )
	return ans
def GCD ( a , b ) :
	if not b :
		return a
	return GCD ( b , a % b )
def findSubseqUtil ( a , b , ans , k , i ) :
	if len ( ans ) == len ( a ) :
		if GCDArr ( ans ) == k :
			print ( ans )
			return True
		else :
			return False
	if not a [ i ] % K :
		ans . append ( a [ i ] )
		temp = findSubseqUtil ( a , b , ans , k , i + 1 )
		if temp == True :
			return True
		ans . pop ( )
	if not b [ i ] % k :
		ans . append ( b [ i ] )
		temp = findSubseqUtil ( a , b , ans , k , i + 1 )
		if temp == True :
			return True
		ans . pop ( )
	return False
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 )