HackerRank Accessory Collection problem solution

In this HackerRank Accessory Collection problem solution we have given L, A, N, and D values for T shopping trips, find and print the maximum amount of money that Victoria can spend during each trip; if it’s not possible for Victoria to make a purchase during a certain trip, print SAD instead. You must print your answer for each trip on a new line.

HackerRank Accessory Collection problem solution

Problem solution in Python.

#!/bin/python3

import os
import sys

#
# Complete the acessoryCollection function below.
#
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()

{“mode”:”full”,”isActive”:false}

Problem solution in Java.

import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;

public class Solution {

    
    static String acessoryCollection(long L, long A, long N, long D) {
        long max=0;
        if(D>N || N>L){
            return "SAD";
        }else if(D==1){
            return String.valueOf(A*L);
        }else{
            long cmid=(N-1)/(D-1);
            for(long mid=cmid;mid>=1;mid--){
                long clarge=N+(mid-1)-((D-1)*mid);
                long n=(L-clarge)/mid;
                long csmall=(L-clarge)%mid;
                if(n>A-1 || (csmall>0 && n==A-1)){
                    break;
                }

                long sum=clarge*A+csmall*(A-n-1)+(((A-1+A-n)*(n) *mid)/2);
                if(sum<max)break;
                max=sum;


            }
        }
        if(max==0){
            return "SAD";
        }else{
            return String.valueOf(max);
        }

    }

    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) throws IOException {
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        int T = Integer.parseInt(scanner.nextLine().trim());

        for (int TItr = 0; TItr < T; TItr++) {
            String[] LAND = scanner.nextLine().split(" ");

            long L = Long.parseLong(LAND[0].trim());

            long A = Long.parseLong(LAND[1].trim());

            long N = Long.parseLong(LAND[2].trim());

            long D = Long.parseLong(LAND[3].trim());

            String result = acessoryCollection(L, A, N, D);

            bufferedWriter.write(result);
            bufferedWriter.newLine();
        }

        bufferedWriter.close();
    }
}

{“mode”:”full”,”isActive”:false}

Problem solution in C++.

#include <bits/stdc++.h>

#include <algorithm>
#include <iostream>
#include <type_traits>
using namespace std;

#define REP1(i, n) for (remove_cv<remove_reference<decltype(n)>::type>::type i = 1; i <= (n); i++)

int main()
{
  long cc, l, a, n, d;
  cin >> cc;
  while (cc--) {
    cin >> l >> a >> n >> d;
    if (d > a)
      cout << "SADn";
    else if (d == 1)
      cout << l*a << endl;
    else {
      long s = -1;
      REP1(i, (n-1)/(d-1)) {
        long x = n-1-(d-1)*i; // extra frequency of A
        if (x+a*i < l) continue;
        long c = (l-x)/i; // number of items >= i
        s = max(s, a*x + (2*a-c+1)*c/2*i + (a-c)*((l-x)%i));
      }
      if (s < 0)
        cout << "SADn";
      else
        cout << s << endl;
    }
  }
}

{“mode”:”full”,”isActive”:false}

Problem solution in C.

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int main(){
    int T; 
    scanf("%d",&T);
    for(int a0 = 0; a0 < T; a0++){
        int L; 
        int A; 
        int N; 
        int D; 
        scanf("%d %d %d %d",&L,&A,&N,&D);
        if (D == 1) {
            printf("%lldn", (long long)A*L);
            continue;
        }
        int max, min, i, q, r;
        long long count, result = 0;
        max = (N-1)/(D-1);
        if ((L-(N-1)+max-1)/max+D-1 > A) {
            printf("SADn");
            continue;
        }
        min = (L-(N-1)+A-(D-1)-1)/(A-(D-1));
        for (i=max; i>=min; i--) {
            q = (L-(N-1))/i;
            r = (L-(N-1))%i;
            count = (long long)(A-(D-1+q))*r + (long long)(A-(D-1+q)+1+A-1)*(D-2+q)/2*i + (long long)A*(N-1-i*(D-2));
            if (count <= result) {
                break;
            }
            result = count;
        }
        printf("%lldn", result);
    }
    return 0;
}
        

{“mode”:”full”,”isActive”:false}