Maximise the number of toys that can be purchased with amount K

27 May, 2021
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 = 50
       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 equals to K.
The image below is an illustration of the above approach:  
Below is the implementation of the above approach:  
C++
Java
Python3
C#
PHP
Javascript
// C++ Program to maximize the// number of toys with K amount#include <bits/stdc++.h>using namespace std; // This functions returns the required// number of toysint maximum_toys(int cost[], int N, int K){    int count = 0, sum = 0;     // sort the cost array    sort(cost, cost + N);    for (int i = 0; i < N; i++) {         // Check if we can buy ith toy or not        if (sum +cost[i] <= K)        {            sum = sum + cost[i];            // Increment count            count++;        }    }    return count;} // Driver Codeint 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 Program to maximize the// number of toys with K amountimport java.io.*;import java .util.*; class GFG{// This functions returns// the required number of toysstatic int maximum_toys(int cost[],                        int N, int K){    int count = 0, sum = 0;     // sort the cost array    Arrays.sort(cost);    for (int i = 0; i < N; i++)    {         // Check if we can buy ith toy or not        if (sum +cost[i] <= K)        {            sum = sum + cost[i];            // Increment count            count++;        }    }    return count;} // Driver Codepublic 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));}} // This code is contributed by anuj_67.
# Python 3 Program to maximize the# number of toys with K amount # This functions returns the required# number of toysdef maximum_toys(cost, N, K):    count = 0    sum = 0     # sort the cost array    cost.sort(reverse = False)    for i in range(0, N, 1):                 # Check if we can buy ith toy or not        if (sum+cost[i] <= K):            sum = sum + cost[i]            # Increment the count variable            count += 1         return count # Driver Codeif __name__ == '__main__':    K = 50    cost = [1, 12, 5, 111, 200,            1000, 10, 9, 12, 15]    N = len(cost)     print(maximum_toys(cost, N, K)) # This code is contributed by# Sanjit_Prasad
// C# Program to maximize the// number of toys with K amountusing System; class GFG{// This functions returns// the required number of toysstatic int maximum_toys(int []cost,                        int N, int K){    int count = 0, sum = 0;     // sort the cost array    Array.Sort(cost);    for (int i = 0; i < N; i++)    {         // Check if we can buy ith toy or not        if (sum +cost[i] <= K)        {            sum = sum + cost[i];            // Increment count            count++;        }    }    return count;} // Driver Codepublic 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));}} // This code is contributed by anuj_67.
<?php// PHP Program to maximize the// number of toys with K amount // This functions returns// the required number of toysfunction maximum_toys($cost, $N, $K){    $count = 0; $sum = 0;     // sort the cost array        sort($cost);    for ($i = 0; $i < $N; $i++)    {             // Check if we can buy ith toy or not        if ($sum + $cost[$i] <= $K)        {            $sum = $sum + $cost[$i];            // Increment the count variable            $count++;        }    }    return $count;} // Driver Code$K = 50;$cost = array(1, 12, 5, 111, 200,            1000, 10, 9, 12, 15 );$N = count($cost); echo maximum_toys($cost, $N, $K),"\n"; // This code is contributed by anuj_67?>
<script>    // Javascript Program to maximize the    // number of toys with K amount         // This functions returns    // the required number of toys    function maximum_toys(cost, N, K)    {        let count = 0, sum = 0;         // sort the cost array        cost.sort(function(a, b){return a - b});        for (let i = 0; i < N; i++)        {             // Check if we can buy ith toy or not            if (sum +cost[i] <= K)            {                sum = sum + cost[i];                // Increment count                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>
6
 
Time Complexity: O(N * logN), where N is the size of the cost array. 
vt_m
Sanjit_Prasad
divyeshrabadiya07
Sorting Quiz
Arrays
Sorting
Arrays
Sorting
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Maximum and minimum of an array using minimum number of comparisons
Top 50 Array Coding Problems for Interviews
Multidimensional Arrays in Java
Stack Data Structure (Introduction and Program)
Linear Search
Merge Sort
Bubble Sort Algorithm
QuickSort
Insertion Sort
Selection Sort Algorithm