Minimum cost for acquiring all coins with k extra coins allowed with every coin
You are given a list of N coins of different denominations. You can pay an amount equivalent to any 1 coin and can acquire that coin. In addition, once you have paid for a coin, we can choose at most K more coins and can acquire those for free. The task is to find the minimum amount required to acquire all the N coins for a given value of K.
Examples :
Input : coin[] = {100, 20, 50, 10, 2, 5}, k = 3 Output : 7 Input : coin[] = {1, 2, 5, 10, 20, 50}, k = 3 Output : 3
As per the question, we can see that at a cost of 1 coin, we can acquire at most K+1 coins. Therefore, in order to acquire all the n coins, we will be choosing ceil(n/(k+1)) coins and the cost of choosing coins will be minimum if we choose the smallest ceil(n/(k+1)) ( Greedy approach). The smallest ceil(n/(k+1)) coins can be found by simply sorting all the N values in increasing order.
If we should check for time complexity (n log n) is for sorting element and (k) is for adding the total amount. So, finally Time Complexity: O(n log n).
C++
// C++ program to acquire all n coins #include<bits/stdc++.h> using namespace std; // function to calculate min cost int minCost( int coin[], int n, int k) { // sort the coins value sort(coin, coin + n); // calculate no. of // coins needed int coins_needed = ceil (1.0 * n / (k + 1)); // calculate sum of // all selected coins int ans = 0; for ( int i = 0; i <= coins_needed - 1; i++) ans += coin[i]; return ans; } // Driver Code int main() { int coin[] = {8, 5, 3, 10, 2, 1, 15, 25}; int n = sizeof (coin) / sizeof (coin[0]); int k = 3; cout << minCost(coin, n, k); return 0; } |
Java
// Java program to acquire // all n coins import java.util.Arrays; class GFG { // function to calculate min cost static int minCost( int coin[], int n, int k) { // sort the coins value Arrays.sort(coin); // calculate no. of // coins needed int coins_needed = ( int )Math.ceil( 1.0 * n / (k + 1 )); // calculate sum of // all selected coins int ans = 0 ; for ( int i = 0 ; i <= coins_needed - 1 ; i++) ans += coin[i]; return ans; } // Driver code public static void main(String arg[]) { int coin[] = { 8 , 5 , 3 , 10 , 2 , 1 , 15 , 25 }; int n = coin.length; int k = 3 ; System.out.print(minCost(coin, n, k)); } } // This code is contributed // by Anant Agarwal. |
Python3
# Python3 program to # acquire all n coins import math # function to calculate min cost def minCost(coin, n, k): # sort the coins value coin.sort() # calculate no. of # coins needed coins_needed = math.ceil( 1.0 * n / / (k + 1 )); # calculate sum of all # selected coins ans = 0 for i in range (coins_needed - 1 + 1 ): ans + = coin[i] return ans # Driver code coin = [ 8 , 5 , 3 , 10 , 2 , 1 , 15 , 25 ] n = len (coin) k = 3 print (minCost(coin, n, k)) # This code is contributed # by Anant Agarwal. |
C#
// C# program to acquire all n coins using System; class GFG { // function to calculate min cost static int minCost( int []coin, int n, int k) { // sort the coins value Array.Sort(coin); // calculate no. of coins needed int coins_needed = ( int )Math.Ceiling(1.0 * n / (k + 1)); // calculate sum of // all selected coins int ans = 0; for ( int i = 0; i <= coins_needed - 1; i++) ans += coin[i]; return ans; } // Driver code public static void Main() { int []coin = {8, 5, 3, 10, 2, 1, 15, 25}; int n = coin.Length; int k = 3; // Function calling Console.Write(minCost(coin, n, k)); } } // This code is contributed // by nitin mittal. |
PHP
<?php // PHP program to acquire all n coins // function to calculate min cost function minCost( $coin , $n , $k ) { // sort the coins value sort( $coin ); sort( $coin , $n ); // calculate no. of coins needed $coins_needed = ceil (1.0 * $n / ( $k + 1)); // calculate sum of // all selected coins $ans = 0; for ( $i = 0; $i <= $coins_needed - 1; $i ++) $ans += $coin [ $i ]; return $ans ; } // Driver Code { $coin = array (8, 5, 3, 10, 2, 1, 15, 25); $n = sizeof( $coin ) / sizeof( $coin [0]); $k = 3; echo minCost( $coin , $n , $k ); return 0; } // This code is contributed // by nitin mittal. ?> |
Javascript
<script> // Javascript program to acquire all n coins // Function to calculate min cost function minCost(coin, n, k) { // Sort the coins value coin.sort( function (a, b){ return a - b}) // Calculate no. of // coins needed var coins_needed = Math.ceil(n /(k + 1)); // Calculate sum of // all selected coins var ans = 0; for ( var i = 0; i <= coins_needed - 1; i++) ans += coin[i]; return ans; } // Driver Code var coin = [ 8, 5, 3, 10, 2, 1, 15, 25 ] var n = coin.length; var k = 3; document.write(minCost(coin, n, k)); // This code is contributed by noob2000 </script> |
Output :
3
Time Complexity: O(n log n)
Auxiliary Space: O(1)
Note that there are more efficient approaches to find the given number of smallest values. For example, method 6 of m largest(or smallest) elements in an array can find m’th smallest element in (n-m) Log m + m Log m).
How to handle multiple queries for a single predefined array?
In this case, if you are asked to find the above answer for many values of K, you have to compute it fast and our time complexity got increased as per the number of queries for k. For the purpose to serve, we can maintain a prefix sum array after sorting all the N values and can answer queries easily and quickly.
Suppose
C++
// C++ program to acquire all // n coins at minimum cost // with multiple values of k. #include<bits/stdc++.h> using namespace std; // Converts coin[] to prefix sum array void preprocess( int coin[], int n) { // sort the coins value sort(coin, coin + n); // Maintain prefix sum array for ( int i = 1; i <= n - 1; i++) coin[i] += coin[i - 1]; } // Function to calculate min // cost when we can get k extra // coins after paying cost of one. int minCost( int coin[], int n, int k) { // calculate no. of coins needed int coins_needed = ceil (1.0 * n / (k + 1)); // return sum of from prefix array return coin[coins_needed - 1]; } // Driver Code int main() { int coin[] = {8, 5, 3, 10, 2, 1, 15, 25}; int n = sizeof (coin) / sizeof (coin[0]); preprocess(coin, n); int k = 3; cout << minCost(coin, n, k) << endl; k = 7; cout << minCost(coin, n, k) << endl; return 0; } |
Java
// C# program to acquire all n coins at // minimum cost with multiple values of k. import java .io.*; import java.util.Arrays; public class GFG { // Converts coin[] to prefix sum array static void preprocess( int []coin, int n) { // sort the coins value Arrays.sort(coin); // Maintain prefix sum array for ( int i = 1 ; i <= n - 1 ; i++) coin[i] += coin[i - 1 ]; } // Function to calculate min cost when we // can get k extra coins after paying // cost of one. static int minCost( int []coin, int n, int k) { // calculate no. of coins needed int coins_needed =( int ) Math.ceil( 1.0 * n / (k + 1 )); // return sum of from prefix array return coin[coins_needed - 1 ]; } // Driver Code static public void main (String[] args) { int []coin = { 8 , 5 , 3 , 10 , 2 , 1 , 15 , 25 }; int n = coin.length; preprocess(coin, n); int k = 3 ; System.out.println(minCost(coin, n, k)); k = 7 ; System.out.println( minCost(coin, n, k)); } } // This code is contributed by anuj_67. |
Python3
# Python3 program to acquire all n coins at # minimum cost with multiple values of k. import math as mt # Converts coin[] to prefix sum array def preprocess(coin, n): # sort the coins values coin.sort() # maintain prefix sum array for i in range ( 1 , n): coin[i] + = coin[i - 1 ] # Function to calculate min cost when we can # get k extra coins after paying cost of one. def minCost(coin, n, k): # calculate no. of coins needed coins_needed = mt.ceil( 1.0 * n / (k + 1 )) # return sum of from prefix array return coin[coins_needed - 1 ] # Driver code coin = [ 8 , 5 , 3 , 10 , 2 , 1 , 15 , 25 ] n = len (coin) preprocess(coin, n) k = 3 print (minCost(coin, n, k)) k = 7 print (minCost(coin, n, k)) # This code is contributed # by Mohit kumar 29 |
C#
// C# program to acquire all n coins at // minimum cost with multiple values of k. using System; public class GFG { // Converts coin[] to prefix sum array static void preprocess( int []coin, int n) { // sort the coins value Array.Sort(coin); // Maintain prefix sum array for ( int i = 1; i <= n - 1; i++) coin[i] += coin[i - 1]; } // Function to calculate min cost when we // can get k extra coins after paying // cost of one. static int minCost( int []coin, int n, int k) { // calculate no. of coins needed int coins_needed =( int ) Math.Ceiling(1.0 * n / (k + 1)); // return sum of from prefix array return coin[coins_needed - 1]; } // Driver Code static public void Main () { int []coin = {8, 5, 3, 10, 2, 1, 15, 25}; int n = coin.Length; preprocess(coin, n); int k = 3; Console.WriteLine(minCost(coin, n, k)); k = 7; Console.WriteLine( minCost(coin, n, k)); } } // This code is contributed by anuj_67. |
PHP
<?php // PHP program to acquire all n coins at // minimum cost with multiple values of k. // Converts coin[] to prefix sum array function preprocess(& $coin , $n ) { // sort the coins value sort( $coin ); // Maintain prefix sum array for ( $i = 1; $i <= $n - 1; $i ++) $coin [ $i ] += $coin [ $i - 1]; } // Function to calculate min cost // when we can get k extra coins // after paying cost of one. function minCost(& $coin , $n , $k ) { // calculate no. of coins needed $coins_needed = ceil (1.0 * $n / ( $k + 1)); // return sum of from prefix array return $coin [ $coins_needed - 1]; } // Driver Code $coin = array (8, 5, 3, 10, 2, 1, 15, 25); $n = sizeof( $coin ); preprocess( $coin , $n ); $k = 3; echo minCost( $coin , $n , $k ) . "\n" ; $k = 7; echo minCost( $coin , $n , $k ) . "\n" ; // This code is contributed by ita_c ?> |
Javascript
<script> // Javascript program to acquire all n coins at // minimum cost with multiple values of k. // Converts coin[] to prefix sum array function preprocess(coin,n) { // sort the coins value coin.sort( function (a,b){ return a-b;}); // Maintain prefix sum array for (let i = 1; i <= n - 1; i++) coin[i] += coin[i - 1]; } // Function to calculate min cost when we // can get k extra coins after paying // cost of one. function minCost(coin,n,k) { // calculate no. of coins needed let coins_needed =Math.ceil(1.0 * n / (k + 1)); // return sum of from prefix array return coin[coins_needed - 1]; } // Driver Code let coin=[8, 5, 3, 10, 2, 1, 15, 25]; let n = coin.length; preprocess(coin, n); let k = 3; document.write(minCost(coin, n, k)+ "<br>" ); k = 7; document.write( minCost(coin, n, k)); // This code is contributed by rag2127 </script> |
Output :
3 1
Time Complexity: O(n log n)
Auxiliary Space: O(1)
After preprocessing, every query for a k takes O(1) time.
Contact Us