Maximise the number of toys that can be purchased with amount K using min Heap
Last Updated :
05 May, 2022
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++
#include <bits/stdc++.h>
using namespace std;
int maxToys( int arr[], int n, int k)
{
priority_queue< int , vector< int >, greater< int > > pq;
for ( int i = 0; i < n; i++) {
pq.push(arr[i]);
}
int count = 0;
while (pq.top() <= k) {
count++;
k = k - pq.top();
pq.pop();
}
return count;
}
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;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
public static int maxToys( int [] arr, int k)
{
int n = arr.length;
PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
for ( int i = 0 ; i < n; i++)
{
pq.offer(arr[i]);
}
int count = 0 ;
while (!pq.isEmpty() && pq.peek() <= k)
{
k = k - pq.poll();
count++;
}
return count;
}
public static void main (String[] args)
{
int [] arr = new int []{ 1 , 12 , 5 , 111 ,
200 , 1000 , 10 };
int k = 50 ;
System.out.println(maxToys(arr, k));
}
}
|
Python3
import heapq
def maxToys(arr, n, k):
pq = arr
heapq.heapify(pq)
count = 0
while (pq[ 0 ] < = k):
count + = 1
k - = pq[ 0 ]
pq[ 0 ] = pq[ - 1 ]
pq.pop()
heapq.heapify(pq)
return count
arr = [ 1 , 12 , 5 , 111 , 200 , 1000 , 10 ]
n = len (arr)
k = 50
print (maxToys(arr, n, k))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int maxToys( int [] arr, int n, int k)
{
List< int > pq = new List< int >();
for ( int i = 0; i < n; i++)
{
pq.Add(arr[i]);
}
pq.Sort();
int count = 0;
while (pq[0] <= k)
{
count++;
k = k - pq[0];
pq.RemoveAt(0);
}
return count;
}
static void Main()
{
int [] arr = { 1, 12, 5, 111, 200, 1000, 10 };
int n = arr.Length;
int k = 50;
Console.WriteLine(maxToys(arr, n, k));
}
}
|
Javascript
<script>
function maxToys(arr, n, k)
{
let pq = [];
for (let i = 0; i < n; i++)
{
pq.push(arr[i]);
}
pq.sort( function (a, b){ return a - b});
let count = 0;
while (pq[0] <= k)
{
count++;
k = k - pq[0];
pq.shift();
}
return count;
}
let arr = [ 1, 12, 5, 111, 200, 1000, 10 ];
let n = arr.length;
let k = 50;
document.write(maxToys(arr, n, k));
</script>
|
Time Complexity: O(N*logN)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...