File tree Expand file tree Collapse file tree 2 files changed +47
-0
lines changed
Expand file tree Collapse file tree 2 files changed +47
-0
lines changed Original file line number Diff line number Diff line change 1+ /*
2+ author:gulshan yadav
3+ email: gulshany01@gmail.com
4+ date: 21-July-2021
5+ */
6+ #include < iostream>
7+ #include < bits/stdc++.h>
8+ using namespace std ;
9+
10+ int purchasingMaxItem (int arr[], int num, int sum){
11+ // build the min heap;
12+ priority_queue<int , vector<int >, greater<int >> pq;
13+
14+ // push all element into heap;
15+ for (int i = 0 ; i< num; i++){
16+ pq.push (arr[i]);
17+ }
18+
19+ int res = 0 ;
20+ while (pq.empty () == false ){
21+ if (pq.top ()<= sum){
22+ res++;
23+ sum = sum-pq.top ();
24+ pq.pop ();
25+ }
26+ else {
27+ break ;
28+ }
29+ }
30+ return res;
31+
32+ }
33+
34+ int main (){
35+ int num;
36+ cin >> num;
37+ int *arr = new int [num];
38+ for (int i = 0 ; i< num; i++){
39+ cin >> arr[i];
40+ }
41+ int sum;
42+ cin >> sum;
43+
44+ cout << purchasingMaxItem (arr, num, sum);
45+ return 0 ;
46+
47+ }
You can’t perform that action at this time.
0 commit comments