Given an array **arr[]** consisting of the cost of toys and an integer **K** depicting the amount of money available to purchase toys. The task is 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:arr[] = {1, 12, 5, 111, 200, 1000, 10, 9, 12, 15}, K = 50

Output:6

Toys with amount 1, 5, 9, 10, 12, and 12

can be purchased resulting in a total amount of 49.

Hence, the maximum number of toys are 6.

Input:arr[] = {1, 12, 5, 111, 200, 1000, 10}, K = 50

Output:4

**Approach:** Insert all the elements of the given array in a priority_queue now one by one remove elements from this priority queue and add these costs in a variable **sum** initialised to **0**. Keep removing the elements while the new addition keep the sum smaller than **K**. In the end, the count of elements removed will be the required answer.

Below is the implementation of the above approach:

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the count of ` `// maximum toys that can be bought ` `int` `maxToys(` `int` `arr[], ` `int` `n, ` `int` `k) ` `{ ` ` ` ` ` `// Create a priority_queue and push ` ` ` `// all the array elements in it ` ` ` `priority_queue<` `int` `, vector<` `int` `>, greater<` `int` `> > pq; ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `pq.push(arr[i]); ` ` ` `} ` ` ` ` ` `// To store the count of maximum ` ` ` `// toys that can be bought ` ` ` `int` `count = 0; ` ` ` `while` `(pq.top() <= k) { ` ` ` `count++; ` ` ` `k = k - pq.top(); ` ` ` `pq.pop(); ` ` ` `} ` ` ` `return` `count; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 1, 12, 5, 111, 200, 1000, 10 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` `int` `k = 50; ` ` ` ` ` `cout << maxToys(arr, n, k); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

4

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Maximise the number of toys that can be purchased with amount K
- Minimum number of equal amount bags to collect at least M money
- Heap Sort for decreasing order using min heap
- Number of ways to form a heap with n distinct integers
- Maximise the sum of two Numbers using at most one swap between them
- Convert min Heap to max Heap
- Maximise the size of consecutive element subsets in an array
- Maximise sum of absolute difference between adjacent elements in Array with sum K
- Maximise array sum after taking non-overlapping sub-arrays of length K
- Select numbers in such way to maximize the amount of money
- Find the maximum amount that can be collected by selling movie tickets
- K-ary Heap
- K’th Least Element in a Min-Heap
- Weak Heap
- Max Heap in Java
- Min Heap in Python
- Max Heap in Python
- Binomial Heap
- Pairing Heap
- Convert BST to Max Heap

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.