Accessory Collection

Sort by

recency

|

22 Discussions

|

  • + 0 comments
    #!/bin/python3
    
    import os
    
    
    def acessoryCollection(L, A, N, D):
        if D > A or N < D or N > L: return "SAD"
        elif D == 1: return str (L * A)
        else:
            max = 0
            a2Max = (N - 1) // (D - 1)
            for a2 in range (a2Max, 0, -1):
                a1 = N + (a2 - 1) - a2 * (D - 1)
                n = (L - a1) // a2
                a3 = (L - a1) % a2
                if n > A - 1 or n == A - 1 and a3 > 0: break
                sum = A * a1 + (A - 1 + A - n) * n // 2 * a2 + a3 * (A - n - 1)
                if sum <= max: break
                max = sum
            if max: return str (max)
            else: return "SAD"
    
    if __name__ == '__main__':
        fptr = open(os.environ['OUTPUT_PATH'], 'w')
    
        T = int(input())
    
        for T_itr in range(T):
            LAND = input().split()
    
            L = int(LAND[0])
    
            A = int(LAND[1])
    
            N = int(LAND[2])
    
            D = int(LAND[3])
    
            result = acessoryCollection(L, A, N, D)
    
            fptr.write(result + '\n')
    
        fptr.close()
    
  • + 0 comments

    Here is my solution in java, javascript, python, C, C++, Csharp HackerRank Accessory Collection Problem Solution

  • + 0 comments

    can anyone find my mistake ? I think it is quite simple.

    string acessoryCollection(int L, int A, int N, int D) {
        long long ans=0;
        if(N<D) ans=-1;
        else if(D==1){
            ans=L*A;
        }
        else{ 
            int min=N-D+1;
            while(L>min){
                if(A==0) {
                    ans=-1;
                    break;
                }
                ans+=min*A;
                A--;
                L-=min;
            }
            if(L>0 & A==0) ans=-1;
            else if(L>0) ans+=L*A;
        }
        //cout << ans<<endl;
        string str;
        while(ans>0){
            str.push_back('0'+ans%10);
            ans=ans/10;
        }
        if(str.size()!=0){
            reverse(str.begin(),str.end());
        }
        
        if(ans==-1) return "SAD";
        else return str;
        
     
    }
    
  • + 0 comments

    Here is the solution of Accessory Collection Click Here

  • + 0 comments

    Here is problem solution - https://programs.programmingoneonone.com/2021/07/hackerrank-accessory-collection-problem-solution.html