-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathmaximize-toys.cpp
More file actions
44 lines (36 loc) · 781 Bytes
/
maximize-toys.cpp
File metadata and controls
44 lines (36 loc) · 781 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// https://practice.geeksforgeeks.org/problems/maximize-toys/0/
#include <bits/stdc++.h>
using namespace std;
int get_max_number_of_toys()
{
int n, k;
cin >> n >> k;
int vect[n];
for(int i =0; i< n; i++)
cin >> vect[i];
sort(vect, vect+n);
if(vect[0] > k)
return 0;
else
{
int counter = 0;
int sum=0;
for(int i=0; i <n ; i++)
{
// Case 1: add another will be too expensive
if(sum + vect[i] > k)
return counter;
counter++;
k -= vect[i];
}
return counter;
}
}
int main()
{
int t;
cin >> t;
while(t--)
cout << get_max_number_of_toys() <<endl;
return 0;
}