-
Notifications
You must be signed in to change notification settings - Fork 51
/
Maximize_Toys.cpp
81 lines (62 loc) · 1.6 KB
/
Maximize_Toys.cpp
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// PS:
// Given an array consisting of the cost of toys.
// Given an integer K depicting the amount of money available to purchase toys.
// Write a program to find the maximum number of toys one can buy with the amount K.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define lt long long
#define pi (3.141592653589)
#define mod 1000000007
#define tc \
int t; \
cin >> t; \
while (t--)
#define lull NULL
#define float double
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(c) c.begin(), c.end()
#define min3(a, b, c) min(c, min(a, b))
#define min4(a, b, c, d) min(d, min(c, min(a, b)))
#define fir(i, n) for (int i = n - 1; i >= 0; i--)
#define fi(i, n) for (int i = 0; i < n; i++)
#define fin(j, n) for (int i = j; i <= n; i++)
#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
// This functions returns the required number of toys
int maximum_toys(vector<int> &cost, int N, int K)
{
int count = 0, sum = 0;
// sort the cost array
sort(cost.begin(), cost.end());
for (int i = 0; i < N; i++)
{
// Check if we can buy ith toy or not
if (sum + cost[i] <= K)
{
sum = sum + cost[i];
// Increment count
count++;
}
}
return count;
}
int main()
{
int k, n;
cin >> n >> k;
vector<int> arr(n);
fi(i, n)
{
cin >> arr[i];
}
cout << maximum_toys(arr, n, k) << endl;
return 0;
}