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 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 minimum amount of money we have to spend to buy all the N different candies.
- Find maximum amount of money we have to spend to buy all the N different candies.

In both the cases we must utilize the offer and get 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 = 2Output :Min = 3, Max = 7Explanation :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.

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 price 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:

Minimum amount :

Maximum amount :

Below is the implementation of the above approach:

## C++

`// C++ implementation to find the minimum` `// and maximum amount` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to find the minimum amount` `// to buy all candies` `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` `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` `int` `main()` `{` ` ` `int` `arr[] = { 3, 2, 1, 4 };` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]);` ` ` `int` `k = 2;` ` ` `sort(arr, arr + n);` ` ` `// Function call` ` ` `cout << findMinimum(arr, n, k) << ` `" "` ` ` `<< findMaximum(arr, n, k) << endl;` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## 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` |

*chevron_right*

*filter_none*

## Python3

`# 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` `(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 ` `=` `[` `3` `, ` `2` `, ` `1` `, ` `4` `]` `n ` `=` `len` `(arr)` `k ` `=` `2` `arr.sort()` `# Function call` `print` `(findMinimum(arr, n, k), ` `" "` `,` ` ` `findMaximum(arr, n, k))` `# This code is contributed` `# by Anant Agarwal.` |

*chevron_right*

*filter_none*

## 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.` |

*chevron_right*

*filter_none*

## 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.` `?>` |

*chevron_right*

*filter_none*

**Output**

3 7

**Time Complexity :** O(n log n)

**Another Implementation:**

We can use the help of The Least integer function (Ceiling function) using built-in ceil() function to implement:

Below is the implementation in Python:

## Python

`# Python implementation` `# to find the minimum` `# and maximum amount` `#import ceil function` `from` `math ` `import` `ceil` `# function to find the maximum` `# and the minimum cost required` `def` `find(arr,n,k): ` ` ` ` ` `# Sort the array` ` ` `arr.sort()` ` ` `b ` `=` `int` `(ceil(n` `/` `k))` ` ` ` ` `# print the minimum cost` ` ` `print` `(` `"minimum "` `,` `sum` `(arr[:b]))` ` ` ` ` `# print the maximum cost` ` ` `print` `(` `"maximum "` `, ` `sum` `(arr[` `-` `b:]));` ` ` ` ` `# Driver Code` `arr ` `=` `[` `3` `, ` `2` `, ` `1` `, ` `4` `]` `n ` `=` `len` `(arr)` `k ` `=` `2` `# Function call` `find(arr,n,k)` |

*chevron_right*

*filter_none*

**Output**

('minimum ', 3) ('maximum ', 7)

This article is contributed by **Sahil Chhabra**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Maximum number of candies that can be bought
- Find minimum number of currency notes and values that sum to given amount
- Buy minimum items without change and given coins
- Buy Maximum Stocks if i stocks can be bought on i-th day
- Distribution of candies according to ages of students
- Maximise the number of toys that can be purchased with amount K
- Select numbers in such way to maximize the amount of money
- Minimum difference between maximum and minimum value of Array with given Operations
- Minimum and Maximum of all subarrays of size K using Map
- Product of all Subsequences of size K except the minimum and maximum Elements
- Split the given array into K sub-arrays such that maximum sum of all sub arrays is minimum
- Find the maximum possible value of a[i] % a[j] over all pairs of i and j
- Find the minimum of maximum length of a jump required to reach the last island in exactly k jumps
- Leftmost and rightmost indices of the maximum and the minimum element of an array
- Find minimum time to finish all jobs with given constraints
- Find the minimum value of the given expression over all pairs of the array
- Find minimum moves to bring all elements in one cell of a matrix
- Choose k array elements such that difference of maximum and minimum is minimized
- Minimize the difference between minimum and maximum elements
- Minimum partitions of maximum size 2 and sum limited by given value