My issue
I have tried to code it as follows-
"def gcd(a,b):
# Find minimum of a and b
result=min(a,b)
while result:
if a%result==0 and b%result==0:
break
result-=1
# Return the gcd of a and b
return result
def initialgcd(lst):
gcdval=lst[0]
for i in lst[1:]:
gcdval=gcd(gcdval,i)
return gcdval
t=int(input())
for i in range(t):
n=int(input())
price=list(map(int,input().split()))
discounted=list(map(int,input().split()))
initialgcdval=initialgcd(price)
maxgcd=initialgcdval
for i in range(len(discounted)):
temp=price[i]
price[i]=discounted[i]
gcd1=initialgcd(price)
maxgcd=max(gcd1,maxgcd)
price[i]=temp
print(maxgcd)"
But I don’t know the way to optimize it
My code
def gcd(a,b):
# Find minimum of a and b
result=min(a,b)
while result:
if a%result==0 and b%result==0:
break
result-=1
# Return the gcd of a and b
return result
def initialgcd(lst):
gcdval=lst[0]
for i in lst[1:]:
gcdval=gcd(gcdval,i)
return gcdval
t=int(input())
for i in range(t):
n=int(input())
price=list(map(int,input().split()))
discounted=list(map(int,input().split()))
initialgcdval=initialgcd(price)
maxgcd=initialgcdval
for i in range(len(discounted)):
temp=price[i]
price[i]=discounted[i]
gcd1=initialgcd(price)
maxgcd=max(gcd1,maxgcd)
price[i]=temp
print(maxgcd)
Problem Link: GCDISCOUNT Problem - CodeChef