Given an array consisting of the cost of toys. Given an integer K depicting the amount of money available to purchase toys. Write a program to find the maximum number of toys one can buy with the amount K.
Note: One can buy only 1 quantity of a particular toy.
Examples:
Input: N = 10, K = 50, cost = { 1, 12, 5, 111, 200, 1000, 10, 9, 12, 15 }
Output: 6
Explanation: Toys with amount 1, 5, 9, 10, 12, and 12 can be purchased resulting in a total amount of 49. Hence, maximum number of toys is 6.
Input: N = 7, K = 5, cost = { 1, 12, 5, 111, 200, 1000, 10 }
Output: 4
The idea to solve this problem is to first sort the cost array in ascending order. This will arrange the toys in increasing order of cost. Now iterate over the cost array and keep calculating the sum of costs until the sum is less than or equal to K. Finally, return the number of toys used to calculate the sum which is just less than or equal to K.
The image below is an illustration of the above approach:

Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maximum_toys( int cost[], int N, int K)
{
int count = 0, sum = 0;
sort(cost, cost + N);
for ( int i = 0; i < N; i++) {
if (sum +cost[i] <= K)
{
sum = sum + cost[i];
count++;
}
}
return count;
}
int main()
{
int K = 50;
int cost[] = { 1, 12, 5, 111, 200, 1000, 10, 9, 12, 15 };
int N = sizeof (cost) / sizeof (cost[0]);
cout << maximum_toys(cost, N, K) << endl;
return 0;
}
|
Java
import java.io.*;
import java .util.*;
class GFG
{
static int maximum_toys( int cost[],
int N, int K)
{
int count = 0 , sum = 0 ;
Arrays.sort(cost);
for ( int i = 0 ; i < N; i++)
{
if (sum +cost[i] <= K)
{
sum = sum + cost[i];
count++;
}
}
return count;
}
public static void main (String[] args)
{
int K = 50 ;
int cost[] = { 1 , 12 , 5 , 111 , 200 ,
1000 , 10 , 9 , 12 , 15 };
int N = cost.length;
System.out.print( maximum_toys(cost, N, K));
}
}
|
Python3
def maximum_toys(cost, N, K):
count = 0
sum = 0
cost.sort(reverse = False )
for i in range ( 0 , N, 1 ):
if ( sum + cost[i] < = K):
sum = sum + cost[i]
count + = 1
return count
if __name__ = = '__main__' :
K = 50
cost = [ 1 , 12 , 5 , 111 , 200 ,
1000 , 10 , 9 , 12 , 15 ]
N = len (cost)
print (maximum_toys(cost, N, K))
|
C#
using System;
class GFG
{
static int maximum_toys( int []cost,
int N, int K)
{
int count = 0, sum = 0;
Array.Sort(cost);
for ( int i = 0; i < N; i++)
{
if (sum +cost[i] <= K)
{
sum = sum + cost[i];
count++;
}
}
return count;
}
public static void Main ()
{
int K = 50;
int []cost = {1, 12, 5, 111, 200,
1000, 10, 9, 12, 15};
int N = cost.Length;
Console.Write( maximum_toys(cost, N, K));
}
}
|
PHP
<?php
function maximum_toys( $cost , $N , $K )
{
$count = 0; $sum = 0;
sort( $cost );
for ( $i = 0; $i < $N ; $i ++)
{
if ( $sum + $cost [ $i ] <= $K )
{
$sum = $sum + $cost [ $i ];
$count ++;
}
}
return $count ;
}
$K = 50;
$cost = array (1, 12, 5, 111, 200,
1000, 10, 9, 12, 15 );
$N = count ( $cost );
echo maximum_toys( $cost , $N , $K ), "\n" ;
?>
|
Javascript
<script>
function maximum_toys(cost, N, K)
{
let count = 0, sum = 0;
cost.sort( function (a, b){ return a - b});
for (let i = 0; i < N; i++)
{
if (sum +cost[i] <= K)
{
sum = sum + cost[i];
count++;
}
}
return count;
}
let K = 50;
let cost = [1, 12, 5, 111, 200, 1000, 10, 9, 12, 15];
let N = cost.length;
document.write(maximum_toys(cost, N, K));
</script>
|
Time Complexity: O(N * logN), where N is the size of the cost array.
Auxiliary Space: O(1) as it is using constant space for variables