human
Can you provide a code to pair with maximum GCD from two arrays and the other steps required given the following:
Pair with maximum GCD from two arrays | Find the maximum GCD pair with maximum sum.
array to keep a count of existing elements.
first [ i ] and second [ i ] are going to store maximum multiples of i in a [ ] and b [ ] respectively ..
traverse through the first array to mark the elements in cnt.
Find maximum multiple of every number in first array.
Find maximum multiple of every number in second array We re - initialise cnt [ ] and traverse through the second array to mark the elements in cnt.
if the multiple is present in the second array then store the max of number or the pre - existing element.
traverse for every elements and checks the maximum N that is present in both the arrays.
driver program to test the above function.
Maximum possible value of elements in both arrays ."
How can the above be solved in Python?
assistant
#Here is code to pair with maximum GCD from two arrays and the other steps required.
 def gcdMax ( a , b , n , N ) :
	cnt = [ 0 ] * N
	first = [ 0 ] * N
	second = [ 0 ] * N
	for i in range ( n ) :
		cnt [ a [ i ] ] = 1
	for i in range ( 1 , N ) :
		for j in range ( i , N , i ) :
			if ( cnt [ j ] ) :
				first [ i ] = max ( first [ i ] , j )
	cnt = [ 0 ] * N
	for i in range ( n ) :
		cnt [ b [ i ] ] = 1
	for i in range ( 1 , N ) :
		for j in range ( i , N , i ) :
			if ( cnt [ j ] > 0 ) :
				second [ i ] = max ( second [ i ] , j )
	i = N - 1
	while i >= 0 :
		if ( first [ i ] > 0 and second [ i ] > 0 ) :
			break
		i -= 1
	print ( str ( first [ i ] ) + " ▁ " + str ( second [ i ] ) )
if __name__ == " _ _ main _ _ " :
	a = [ 3 , 1 , 4 , 2 , 8 ]
	b = [ 5 , 2 , 12 , 8 , 3 ]
	n = len ( a )
	N = 20
	gcdMax ( a , b , n , N )