Sort by

recency

|

5 Discussions

|

  • + 0 comments

    C++ solution

    #include <bits/stdc++.h>
    
    using namespace std;
    
    int n;
    vector<long long int> v;
    vector<pair<long long int, pair<long long int, long long int>>> vl;
    vector<pair<long long int, pair<long long int, long long int>>> vr;
    map<long long int, long long int> mp;
    long long int ans;
    
    long long int gcd(long long int a, long long int b){
        if(a > b){
            swap(a, b);
        }
        while(a){
            swap(a, b);
            a %= b;
        }
        return b;
    }
    
    long long int lcm(long long int a, long long int b){
        return a / gcd(a, b) * b;
    }
    
    inline void solve(int l, int r){
        int mid = (l + r) >> 1;
        {
            vl.clear();
            long long int sum = 0;
            long long int mx = v[mid];
            int gc = 0;
            for(int j = mid; j <= r; j++){
                sum += v[j];
                gc = gcd(gc, abs(v[j]) );
                mx = max(mx, v[j]);
                ans = max(ans, gc*(sum - mx));
            vl.push_back(make_pair(mx, make_pair(sum, gc)));
            }
        }
        {
            vr.clear();
            long long int sum = 0;
            long long int mx = v[mid];
            int gc = 0;
            for(int j = mid; j >=l; j--){
                sum += v[j];
                gc = gcd(gc, abs(v[j]) );
                mx = max(mx, v[j]);
                ans = max(ans, gc*(sum - mx));
            vr.push_back(make_pair(mx, make_pair(sum, gc)));
            }
        }
        sort(vl.begin(), vl.end());
        sort(vr.begin(), vr.end());
        int idx = 0;
        mp.clear();
        for(int i = 0; i < vl.size(); i++){
            while(idx < vr.size() && vr[idx].first <= vl[i].first){
                int gc = vr[idx].second.second;
                if(mp.count(gc) == 0){
                    mp[gc] = LLONG_MIN;
                }
                mp[gc] = max(mp[gc], vr[idx].second.first-v[mid]);
                idx++;
            }
            for(auto it = mp.begin(); it != mp.end(); it++){
                long long int G = gcd((*it).first, vl[i].second.second);
                long long int SUM = (*it).second + vl[i].second.first;
                long long int MX = vl[i].first;
                G *= (SUM - MX);
                ans = max(ans, G);
            }
        }
        mp.clear();
        swap(vl, vr);
        idx = 0;
        for(int i = 0; i < vl.size(); i++){
            while(idx < vr.size() && vr[idx].first <= vl[i].first){
                int gc = vr[idx].second.second;
                if(mp.count(gc) == 0){
                    mp[gc] = LLONG_MIN;
                }
                mp[gc] = max(mp[gc], vr[idx].second.first - v[mid]);
                idx++;
            }
            for(auto it = mp.begin(); it != mp.end(); it++){
                long long int G = gcd((*it).first, vl[i].second.second);
                long long int SUM = (*it).second + vl[i].second.first;
                long long int MX = vl[i].first;
                G *= (SUM - MX);
                ans = max(ans, G);
            }
        }
        if(l <= mid - 1) solve(l, mid - 1);
        if(mid + 1 <= r) solve(mid + 1, r);
    }
    int main(){
        cin >> n;
        for(int i = 0; i < n; i++){
            int a;
            scanf("%d", &a);
            v.push_back(a);
        }
        solve(0, n - 1);
        printf("%lld\n", ans);
        return 0;
    }
    
  • + 0 comments

    Getting RunTime Errors

    #!/bin/python3
    
    import math
    import os
    import random
    import re
    import sys
    
    def gcd(x, y):
        x,y = abs(x),abs(y)
        while(y): 
            x, y = y, x % y 
        return x
    
    def gcdAr(a):
        if len(a) == 2:
            return gcd(a[0],a[1])
        return gcd(a[0],gcdAr(a[1:]))
    # Complete the maximumValue function below.
    def maximumValue(a):
        l = len(a)
        
        mat = [[0]*l for i in range(l)]
        matSum = [[0]*l for i in range(l)]
        matMax = [[0]*l for i in range(l)]
        
        ma = -float('inf')
        
        for i in range(l):
            mat[i][i] = abs(a[i])
            matSum[i][i] = a[i]
            matMax[i][i] = a[i]
    
        for i in range(l):
            for j in range(i+1,l):
                mat[i][j] = gcd(a[j],mat[i][j-1])
                
        for i in range(l):
            for j in range(i+1,l):
                matSum[i][j] = matSum[i][j-1] + a[j]
        print(matSum)
        for i in range(l):
            for j in range(i+1,l):
                matMax[i][j] = max(matMax[i][j-1],a[j])
        print(matMax)
        for i in range(l):
            for j in range(i,l):
                f = mat[i][j]*(matSum[i][j] - matMax[i][j])
                if f > ma:
                    ma = f
        return ma
                
    
    if __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()
    
  • + 0 comments

    can some one please explain me this problem ?

  • + 0 comments

    here is hackerrank the strange function solution

  • + 1 comment
    from itertools import combinations
    n=int(input())
    l=list(map(int,input().split(" ")))
    o=[0]*len(l)
    def find_gcd(x,y):
        while (y):
            x,y=y,x % y
        return x
    for (i,j) in combinations(range(1,len(l)+1),2):
        if i <= j:
            s=l[i-1:j]
            if len(s)==1:gcd=abs(s[0])   
            else :
                gcd=find_gcd(abs(s[0]),abs(s[1]))        
                for k in range(2,len(s)):
                    gcd = find_gcd(gcd,abs(s[k])) 
            summt=(sum(s)-max(s))
            o.append(gcd*summt)
    print(max(o))  
    

No more comments