Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerRank Greedy Florist problem solution

YASH PAL, 31 July 20247 February 2026

In this HackerRank Greedy Florist problem solution, given the size of the group of friends, the number of flowers they want to purchase and the original prices of the flowers, determine the minimum cost to purchase all of the flowers. The number of flowers they want equals the length of the c array.

Function Description

Complete the getMinimumCost function in the editor below.

getMinimumCost has the following parameter(s):

  • int c[n]: the original price of each flower
  • int k: the number of friends

Returns
– int: the minimum cost to purchase all flowers

HackerRank Greedy Florist Interview preparation kit solution

HackerRank Greedy Florist problem solution in Python.

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)

Greedy Florist problem solution in Java.

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("(rn|[nru2028u2029u0085])?");

        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("%Ldn",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("%dn", 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);
});
coding problems solutions Hackerrank Problems Solutions interview prepration kit HackerRank

Post navigation

Previous post
Next post

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes