Created
July 20, 2017 12:33
-
-
Save divyamani1/888f714f49dd1c2bf47a620586208410 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Need to reduce the time complexity.