Skip to content

Instantly share code, notes, and snippets.

@divyamani1
Created July 20, 2017 12:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save divyamani1/888f714f49dd1c2bf47a620586208410 to your computer and use it in GitHub Desktop.
Save divyamani1/888f714f49dd1c2bf47a620586208410 to your computer and use it in GitHub Desktop.
#!/bin/python3
import sys
from fractions import gcd
from itertools import product
def maximumGcdAndSum(A, B):
gcd_max=0
x_max=0
y_max=0
for x,y in product(A,B):
z=gcd(x,y)
if(z>gcd_max):
gcd_max=z
x_max=x
y_max=y
elif(z==gcd_max):
s_new=x+y
if(s_new>=(x_max+y_max)):
x_max=x
y_max=y
sum=x_max+y_max
return sum
if __name__ == "__main__":
n = int(input().strip())
list_A = list(map(int, input().strip().split(' ')))
list_B = list(map(int, input().strip().split(' ')))
A=list(set(list_A))
B=list(set(list_B))
res = maximumGcdAndSum(A, B)
print(res)
@divyamani1
Copy link
Author

Need to reduce the time complexity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment