Minimum cost for acquiring all coins with k extra coins allowed with every coin | Function to calculate min cost ; Sort the coins value ; Calculate no . of coins needed ; Calculate sum of all selected coins ; Driver Code
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 ) ) ;