jayati

Maximize Toys

Oct 25th, 2023
1,587
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.06 KB | None | 0 0
  1. //{ Driver Code Starts
  2. //Initial Template for C++
  3.  
  4. #include <bits/stdc++.h>
  5. using namespace std;
  6.  
  7.  
  8. // } Driver Code Ends
  9. //User function Template for C++
  10.  
  11. class Solution{
  12. public:
  13.     int toyCount(int N, int K, vector<int> arr)
  14.     {
  15.         // code here
  16.         vector<int> smaller;
  17.         for(int i=0;i<N;i++)
  18.         {
  19.             if(arr[i]<K)
  20.             {
  21.                 smaller.push_back(arr[i]);
  22.             }
  23.         }
  24.         sort(smaller.begin(),smaller.end());
  25.         int sum=0;
  26.         int c=0;
  27.         for(int i=0;i<smaller.size();i++)
  28.         {
  29.             if(sum+smaller[i]<=K)
  30.             {
  31.                 c++;
  32.                 sum+=smaller[i];
  33.             }
  34.         }
  35.         return c;
  36.     }
  37. };
  38.  
  39. //{ Driver Code Starts.
  40.  
  41. int main(){
  42.     int t;
  43.     cin>>t;
  44.     while(t--){
  45.         int N, K;
  46.         cin>>N>>K;
  47.         vector<int> arr(N);
  48.         for(int i = 0;i < N;i++)
  49.             cin>>arr[i];
  50.        
  51.         Solution ob;
  52.         cout<<ob.toyCount(N, K, arr)<<endl;
  53.     }
  54.     return 0;
  55. }
  56. // } Driver Code Ends
Advertisement
Add Comment
Please, Sign In to add comment