title: Maximum Gcd and Sum
body: <blockquote>
    <p>You are given two arrays <em>A</em> and <em>B</em> containing <em>n</em> elements each. Choose a pair of elements (<em>x</em>, <em>y</em>) such that:<br>
    • <em>x</em> belongs to Array <em>A</em><br>
    • <em>y</em> belongs to Array <em>B</em><br>
    • GCD(<em>x</em>, <em>y</em>) is the maximum of all pairs (<em>x</em>, <em>y</em>).<br>
    If there is more than one such pair having maximum gcd, then choose the one with maximum sum. Print the sum of elements of this maximum-sum pair. </p>
  </blockquote>
  
  <p>This is question from Hackerrank <a href="https://www.hackerrank.com/contests/w34/challenges/maximum-gcd-and-sum" rel="nofollow noreferrer">weekofcode</a> 34.</p>
  
  <pre><code>from fractions import gcd
  
  from itertools import product
  n = int(input().strip()) #two arrays of equal length
  A = set(map(int, input().strip().split(' '))) #array1
  B = set(map(int, input().strip().split(' '))) # arry2
  output_sum=[]
  output_GCD=[]
  c=list(product(A,B))
  for i in c:
  
      temp1=i[0]
      temp2=i[1]
  
      sum_two=temp1+temp2
  
      temp3=gcd(temp1,temp2)
      output_GCD.append(temp3)
      output_sum.append(temp1+temp2)
  temp=[]
  for i in range(len(output_GCD)):
    if(output_GCD[i]==max(output_GCD)):
      temp.append(output_sum[i])
  print(max(temp))
  </code></pre>
  
  <p>This solution works for smaller conditions and I got timed out for most of the test cases, please help me how to improve my solution.</p>