In this HackerRank Greedy Florist interview preparation kit problem, you need to Complete the getMinimumCost function in the editor.


HackerRank Greedy Florist Interview preparation kit solution


Problem solution in Python programming.

N, k = [int(i) for i in input().split(' ')]
ci = [int(i) for i in input().split(' ')] 

ci.sort()

if N<len(ci):
	N = len(ci)

totalCount, perPerson, curRound, totalAmount = 0, 0, 0, 0
pFlower = len(ci) - 1
while totalCount < N:
	totalAmount += ci[pFlower]*(perPerson+1)
	
	curRound += 1
	if curRound == k:
		curRound = 0
		perPerson += 1
	totalCount += 1
	pFlower -= 1

print(totalAmount)


Problem solution in Java Programming.

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

public class Solution {

    // Complete the getMinimumCost function below.
    static int getMinimumCost(int k, int[] c) {
                        Arrays.sort(c);
                        int ans=0;
                        int count=1;
                        int noOfFriendsRem=k;
                    for (int i = c.length-1; i >-1; i--) {
                            if (noOfFriendsRem==0) {
                                    noOfFriendsRem=k;
                                    count++;
                            }
                            ans+=c[i]*count;
        noOfFriendsRem--;
    }
    return ans;

}

    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")));

        String[] nk = scanner.nextLine().split(" ");

        int n = Integer.parseInt(nk[0]);

        int k = Integer.parseInt(nk[1]);

        int[] c = new int[n];

        String[] cItems = scanner.nextLine().split(" ");
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

        for (int i = 0; i < n; i++) {
            int cItem = Integer.parseInt(cItems[i]);
            c[i] = cItem;
        }

        int minimumCost = getMinimumCost(k, c);

        bufferedWriter.write(String.valueOf(minimumCost));
        bufferedWriter.newLine();

        bufferedWriter.close();

        scanner.close();
    }
}


Problem solution in C++ programming.

#include <cstdio>
#include <algorithm>
using namespace std;

int a[102];

int main() {
int i,j,m,n;
long long c,ans=0;
	scanf("%d%d",&n,&m);
	for (i=0;i<n;++i) {
		scanf("%d",&a[i]);
	}
	sort(a,a+n);
	for (i=n-1,c=0,j=0;i>=0;--i) {
		if (j==0) {
			++c;
		}
		ans+=c*a[i];
		if (++j==m) {
			j=0;
		}
	}
	printf("%Ld\n",ans);
	return 0;
}

Problem solution in C programming.


#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX 101

int cmp(const void *a, const void *b)
{
    return *(int *)b - *(int *)a;
}

int getPrice(int arr, int cus[], int ix)
{
    return (cus[ix]+1)*arr;
}

int main(void)
{
    int i, ix;
    int N, K, sum;
    int arr[MAX], cus[MAX];

    memset(arr, 0, sizeof(arr));
    memset(cus, 0, sizeof(cus));
    scanf("%d %d", &N, &K);

    for(i = 0; i < N; ++i)
        scanf("%d", &arr[i]);

    qsort(arr, N, sizeof(int), cmp);

    sum = 0; ix = 0;
    for(i = 0; i < N; ++i){
        sum += getPrice(arr[i], cus, ix%K);
        ++cus[ix%K];
        ix = (++ix)%K;
    }

    printf("%d\n", sum);

    return 0;
}


Problem solution in JavaScript programming.

process.stdin.resume();
process.stdin.setEncoding("ascii");
process.stdin.on("data", function (input) {
    function sortNumber(a,b) {
    return b - a;
    }
    var a = input.split("\n");
    b = a[0];
    c = a[1];
    var d = b.split(" ");
    var n = d[0];
    var k = d[1];
    var prices = c.split(" ");
    var op = prices.sort(sortNumber);
    var total=0;
    for (var i=0; i<op.length; i++){
        var r = i/k;
        var purchase = Math.floor(r);
        total = total+((purchase+1)*op[i]);
    }
    process.stdout.write(total);
});