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++
#include<bits/stdc++.h>
using namespace std;
int minCost( int coin[], int n, int k)
{
sort(coin, coin + n);
int coins_needed = ceil (1.0 * n /
(k + 1));
int ans = 0;
for ( int i = 0; i <= coins_needed - 1;
i++)
ans += coin[i];
return ans;
}
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
import java.util.Arrays;
class GFG
{
static int minCost( int coin[],
int n, int k)
{
Arrays.sort(coin);
int coins_needed = ( int )Math.ceil( 1.0 *
n / (k + 1 ));
int ans = 0 ;
for ( int i = 0 ; i <= coins_needed - 1 ;
i++)
ans += coin[i];
return ans;
}
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));
}
}
|
Python3
import math
def minCost(coin, n, k):
coin.sort()
coins_needed = math.ceil( 1.0 * n / /
(k + 1 ));
ans = 0
for i in range (coins_needed - 1 + 1 ):
ans + = coin[i]
return ans
coin = [ 8 , 5 , 3 , 10 ,
2 , 1 , 15 , 25 ]
n = len (coin)
k = 3
print (minCost(coin, n, k))
|
C#
using System;
class GFG
{
static int minCost( int []coin,
int n, int k)
{
Array.Sort(coin);
int coins_needed = ( int )Math.Ceiling(1.0 *
n / (k + 1));
int ans = 0;
for ( int i = 0; i <= coins_needed - 1; i++)
ans += coin[i];
return ans;
}
public static void Main()
{
int []coin = {8, 5, 3, 10,
2, 1, 15, 25};
int n = coin.Length;
int k = 3;
Console.Write(minCost(coin, n, k));
}
}
|
PHP
<?php
function minCost( $coin , $n , $k )
{
sort( $coin ); sort( $coin , $n );
$coins_needed = ceil (1.0 * $n / ( $k + 1));
$ans = 0;
for ( $i = 0; $i <= $coins_needed - 1; $i ++)
$ans += $coin [ $i ];
return $ans ;
}
{
$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;
}
?>
|
Javascript
<script>
function minCost(coin, n, k)
{
coin.sort( function (a, b){ return a - b})
var coins_needed = Math.ceil(n /(k + 1));
var ans = 0;
for ( var i = 0; i <= coins_needed - 1; i++)
ans += coin[i];
return ans;
}
var coin = [ 8, 5, 3, 10,
2, 1, 15, 25 ]
var n = coin.length;
var k = 3;
document.write(minCost(coin, n, k));
</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++
#include<bits/stdc++.h>
using namespace std;
void preprocess( int coin[], int n)
{
sort(coin, coin + n);
for ( int i = 1; i <= n - 1; i++)
coin[i] += coin[i - 1];
}
int minCost( int coin[], int n, int k)
{
int coins_needed = ceil (1.0 * n / (k + 1));
return coin[coins_needed - 1];
}
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
import java .io.*;
import java.util.Arrays;
public class GFG {
static void preprocess( int []coin, int n)
{
Arrays.sort(coin);
for ( int i = 1 ; i <= n - 1 ; i++)
coin[i] += coin[i - 1 ];
}
static int minCost( int []coin, int n, int k)
{
int coins_needed =( int ) Math.ceil( 1.0
* n / (k + 1 ));
return coin[coins_needed - 1 ];
}
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));
}
}
|
Python3
import math as mt
def preprocess(coin, n):
coin.sort()
for i in range ( 1 , n):
coin[i] + = coin[i - 1 ]
def minCost(coin, n, k):
coins_needed = mt.ceil( 1.0 * n / (k + 1 ))
return coin[coins_needed - 1 ]
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))
|
C#
using System;
public class GFG {
static void preprocess( int []coin, int n)
{
Array.Sort(coin);
for ( int i = 1; i <= n - 1; i++)
coin[i] += coin[i - 1];
}
static int minCost( int []coin, int n, int k)
{
int coins_needed =( int ) Math.Ceiling(1.0
* n / (k + 1));
return coin[coins_needed - 1];
}
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));
}
}
|
PHP
<?php
function preprocess(& $coin , $n )
{
sort( $coin );
for ( $i = 1; $i <= $n - 1; $i ++)
$coin [ $i ] += $coin [ $i - 1];
}
function minCost(& $coin , $n , $k )
{
$coins_needed = ceil (1.0 * $n / ( $k + 1));
return $coin [ $coins_needed - 1];
}
$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" ;
?>
|
Javascript
<script>
function preprocess(coin,n)
{
coin.sort( function (a,b){ return a-b;});
for (let i = 1; i <= n - 1; i++)
coin[i] += coin[i - 1];
}
function minCost(coin,n,k)
{
let coins_needed =Math.ceil(1.0
* n / (k + 1));
return coin[coins_needed - 1];
}
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));
</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.
This article is contributed by Shivam Pradhan (anuj_charm). If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@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.
Please Login to comment...