We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
#!/bin/python3importmathimportosimportrandomimportreimportsysdefgcd(x,y):x,y=abs(x),abs(y)while(y):x,y=y,x%yreturnxdefgcdAr(a):iflen(a)==2:returngcd(a[0],a[1])returngcd(a[0],gcdAr(a[1:]))# Complete the maximumValue function below.defmaximumValue(a):l=len(a)mat=[[0]*lforiinrange(l)]matSum=[[0]*lforiinrange(l)]matMax=[[0]*lforiinrange(l)]ma=-float('inf')foriinrange(l):mat[i][i]=abs(a[i])matSum[i][i]=a[i]matMax[i][i]=a[i]foriinrange(l):forjinrange(i+1,l):mat[i][j]=gcd(a[j],mat[i][j-1])foriinrange(l):forjinrange(i+1,l):matSum[i][j]=matSum[i][j-1]+a[j]print(matSum)foriinrange(l):forjinrange(i+1,l):matMax[i][j]=max(matMax[i][j-1],a[j])print(matMax)foriinrange(l):forjinrange(i,l):f=mat[i][j]*(matSum[i][j]-matMax[i][j])iff>ma:ma=freturnmaif__name__=='__main__':fptr=open(os.environ['OUTPUT_PATH'],'w')n=int(input())a=list(map(int,input().rstrip().split()))result=maximumValue(a)fptr.write(str(result)+'\n')fptr.close()
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
The Strange Function
You are viewing a single comment's thread. Return to all comments →
Getting RunTime Errors