Maximize Toys | Practice | GeeksforGeeks

Given an array arr[ ] of length N consisting cost of N toys and an integer K depicting the amount with you. Your task is to find maximum number of toys you can buy with K amount. 
Example 1:
Input: 
N = 7 
K = 50
arr[] = {1, 12, 5, 111, 200, 1000, 10}
Output: 4
Explaination: The costs of the toys 
you can buy are 1, 12, 5 and 10.
Example 2:
Input: 
N = 3 
K = 100
arr[] = {20, 30, 50}
Output: 3
Explaination: You can buy all toys.
Your Task:
You do not need to read input or print anything. Your task is to complete the function toyCount() which takes the integer N, integer K and the array arr[ ] as input parameters and returns the maximum count of toys you can have.
Expected Time Complexity: O(N * Log(N))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 105
1 ≤ K, arr[i] ≤ 109
0
akashkhurana283 days ago
JAVA PRIORITY QUEUE
  PriorityQueue<Integer> pq=new PriorityQueue<Integer>();              int res=0;       for(int i=0;i<N;i++)           pq.add(arr[i]);              for(int i=0;i<N;i++){           if(pq.peek()<=K){               K-=pq.poll();               res++;           }else{               break;           }       }       return res;       } }
 
0
19bcs13142 weeks ago
// Greedy Approach


class Solution{
    static int toyCount(int n, int k, int arr[])
    {
        // code here
        Arrays.sort(arr);
        
        int count=0;
        for(int i=0;i<n;i++){
            if(arr[i]<=k){
                k=k-arr[i];
                count++;
            }else{
                break;
            }
        }
        return count;
    }
}
0
abytespaceneeded3 weeks ago
class Solution{
public:
    int toyCount(int N, int K, vector<int> arr)
    {
        // code here
        priority_queue<int, vector<int>, greater<int>> pq(arr.begin(),arr.end());
        
        int item = 0;
        
        while(pq.empty() == false)
        {
            if(pq.top() <= K)
            {
                ++item;
                K = K-pq.top();
                pq.pop();
            }
            else
                break;
        }
        
        return item;
    }
};
0
abytespaceneeded
This comment was deleted.
0
mitradiptamoy3 weeks ago
//C++ soln
 
int toyCount(int N, int K, vector<int> arr)   {       // code here       sort(arr.begin(),arr.end());       int i=0;       int x=0;       int s=0;       while(i<N){           s+=arr[i];           if(s<=K)           {               x++;           }           else           {               break;           }           i++;       }    return x;      }
0
madhukartemba3 weeks ago
JAVA SOLUTION:
class Solution
{
    static int toyCount(int N, int K, int arr[])
    {
        Arrays.sort(arr);
        
        int count = 0;
        
        for(int i=0; i<N; i++)
        {
            if(K>=arr[i]) 
            {
                K-=arr[i];
                count++;
            }
            else return count;
        }
        
        return count;
    }
}
0
hpankur023 weeks ago
//Python Easy solution
       arr.sort()       count=0              for i in range(0,N):           if(arr[i]<=K ):               count+=1               K=K-arr[i]              return count
0
gauralkshirsagar54 weeks ago
int sum=0;       int count =0;       sort(arr.begin(),arr.end());       for(int i=0;i<=N-1;++i){           sum=arr[i]+sum;           if(sum>K)           break;           count++;       }       return count;   }
0
gupta2411sumit1 month ago
//Easy C++ Solution
int toyCount(int N, int k, vector<int> arr)   {       // code here              sort( arr.begin() , arr.end()) ;       int count = 0 ;       long long int cost = 0 ;       int i = 0 ;              while(cost<k && i<N)       {           cost += arr[i] ;                      if(cost<=k)           {               count++ ;           }           i++ ;       }              return count ;   }
0
mummadiug1 month ago
def toyCount(self, N, K, arr):       # code here       arr=sorted(arr)       c=0       for i in range(N):           if arr[i]<=K:               c+=1               K=K-arr[i]       return c
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.