Min and Max Costs to Buy All
Last Updated :
25 Oct, 2024
In a candy store, there are N different types of candies available and the prices of all the N different types of candies are provided. There is also an attractive offer by the candy store. We can buy a single candy from the store and get at most K other candies (all are different types) for free.
- Find the minimum amount of money we have to spend to buy all the N different candies.
- Find the maximum amount of money we have to spend to buy all the N different candies.
In both cases, we must utilize the offer and get the maximum possible candies back. If k or more candies are available, we must take k candies for every candy purchase. If less than k candies are available, we must take all candies for a candy purchase.
Examples:
Input : price[] = {3, 2, 1, 4}, k = 2
Output : Min = 3, Max = 7
Explanation :
Since k is 2, if we buy one candy we can take atmost two more for free. So in the first case we buy the candy which costs 1 and take candies worth 3 and 4 for free, also you buy candy worth 2 as well. So min cost = 1 + 2 = 3.
In the second case we buy the candy which costs 4 and take candies worth 1 and 2 for free, also We buy candy worth 3 as well. So max cost = 3 + 4 = 7.
Input: n = 5, k = 4, candies[] = {3 2 1 4 5}
Output: 1 5
Explanation: For minimimum cost buy the candy with the cost 1 and get all the other candies for free. For maximum cost buy the candy with the cost 5 and get all other candies for free.
One important thing to note is, we must use the offer and get maximum candies back for every candy purchase. So if we want to minimize the money, we must buy candies at minimum cost and get candies of maximum costs for free. To maximize the money, we must do the reverse. Below is an algorithm based on this.
First Sort the given array.
For finding minimum amount : Start purchasing candies from starting and reduce k free candies from last with every single purchase.
For finding maximum amount : Start purchasing candies from the end and reduce k free candies from starting in every single purchase.
Below image is an illustration of the above approach:

Maximum amount :

Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
// Function to find the minimum and maximum amount
// to buy all candies
vector<int> findMinAndMax(vector<int>& arr, int k) {
// Sort the array to arrange candies by price
sort(arr.begin(), arr.end());
// Calculate minimum cost by traversing
// from beginning
int n = arr.size();
int minCost = 0;
for (int i = 0; i < n; i++) {
minCost += arr[i];
n -= k;
}
// calculate maximum cost by traversing
// from end
int index = 0, maxCost = 0;
for (int i = arr.size() - 1; i >= index; i--) {
maxCost += arr[i];
index += k;
}
return {minCost, maxCost};
}
int main() {
vector<int> arr = {3, 2, 1, 4};
int k = 2;
vector<int> res = findMinAndMax(arr, k);
cout << res[0] << " " << res[1] << endl;
return 0;
}
Java
// Java implementation to find the
// minimum and maximum amount
import java.util.*;
class GFG {
// Function to find the minimum
// amount to buy all candies
static int findMinimum(int arr[], int n, int k)
{
int res = 0;
for (int i = 0; i < n; i++) {
// Buy current candy
res += arr[i];
// And take k candies for free
// from the last
n = n - k;
}
return res;
}
// Function to find the maximum
// amount to buy all candies
static int findMaximum(int arr[], int n, int k)
{
int res = 0, index = 0;
for (int i = n - 1; i >= index; i--)
{
// Buy candy with maximum amount
res += arr[i];
// And get k candies for free from
// the starting
index += k;
}
return res;
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 3, 2, 1, 4 };
int n = arr.length;
int k = 2;
Arrays.sort(arr);
// Function call
System.out.println(findMinimum(arr, n, k) + " "
+ findMaximum(arr, n, k));
}
}
// This code is contributed by prerna saini
Python
# Python implementation
# to find the minimum
# and maximum amount
# Function to find
# the minimum amount
# to buy all candies
def findMinimum(arr, n, k):
res = 0
i = 0
while(i<n):
# Buy current candy
res += arr[i]
# And take k
# candies for free
# from the last
n = n-k
i += 1
return res
# Function to find
# the maximum amount
# to buy all candies
def findMaximum(arr, n, k):
res = 0
index = 0
i = n-1
while(i >= index):
# Buy candy with
# maximum amount
res += arr[i]
# And get k candies
# for free from
# the starting
index += k
i -= 1
return res
# Driver code
arr = [1,2,3,4,5,6,7,8,9,10]
n = len(arr)
k = 0
arr.sort()
# Function call
print(findMinimum(arr, n, k), " ",
findMaximum(arr, n, k))
# This code is contributed
# by Anant Agarwal.
C#
// C# implementation to find the
// minimum and maximum amount
using System;
public class GFG {
// Function to find the minimum
// amount to buy all candies
static int findMinimum(int[] arr, int n, int k)
{
int res = 0;
for (int i = 0; i < n; i++)
{
// Buy current candy
res += arr[i];
// And take k candies for
// free from the last
n = n - k;
}
return res;
}
// Function to find the maximum
// amount to buy all candies
static int findMaximum(int[] arr, int n, int k)
{
int res = 0, index = 0;
for (int i = n - 1; i >= index; i--)
{
// Buy candy with maximum
// amount
res += arr[i];
// And get k candies for free
// from the starting
index += k;
}
return res;
}
// Driver code
public static void Main()
{
int[] arr = { 3, 2, 1, 4 };
int n = arr.Length;
int k = 2;
Array.Sort(arr);
// Function call
Console.WriteLine(findMinimum(arr, n, k) + " "
+ findMaximum(arr, n, k));
}
}
// This code is contributed by Sam007.
JavaScript
<script>
// Javascript implementation to find the
// minimum and maximum amount
// Function to find the minimum
// amount to buy all candies
function findMinimum(arr,n,k)
{
let res = 0;
for(let i = 0; i < n; i++)
{
// Buy current candy
res += arr[i];
// And take k candies for free
// from the last
n = n - k;
}
return res;
}
// Function to find the maximum
// amount to buy all candies
function findMaximum(arr,n,k)
{
let res = 0, index = 0;
for(let i = n - 1; i >= index; i--)
{
// Buy candy with maximum amount
res += arr[i];
// And get k candies for free from
// the starting
index += k;
}
return res;
}
// Driver code
let arr = [ 3, 2, 1, 4 ];
let n = arr.length;
let k = 2;
arr.sort(function(a, b){return a - b;});
// Function call
document.write(findMinimum(arr, n, k) + " " +
findMaximum(arr, n, k));
// This code is contributed by patel2127
</script>
PHP
<?php
// PHP implementation to find the minimum
// and maximum amount
// Function to find the minimum amount
// to buy all candies
function findMinimum($arr, $n,$k)
{
$res = 0;
for ($i = 0; $i < $n ; $i++)
{
// Buy current candy
$res += $arr[$i];
// And take k candies for free
// from the last
$n = $n - $k;
}
return $res;
}
// Function to find the maximum amount
// to buy all candies
function findMaximum($arr, $n, $k)
{
$res = 0;
$index = 0;
for ($i = $n - 1; $i >= $index; $i--)
{
// Buy candy with maximum amount
$res += $arr[$i];
// And get k candies
// for free from
// the starting
$index += $k;
}
return $res;
}
// Driver Code
$arr = array(3, 2, 1, 4);
$n = sizeof($arr);
$k = 2;
sort($arr); sort($arr,$n);
// Function call
echo findMinimum($arr, $n, $k)," "
,findMaximum($arr, $n, $k);
return 0;
// This code is contributed by nitin mittal.
?>
Time Complexity : O(n log n)
Auxiliary Space: O(1)