forked from projectX-india/madlab-hack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSecurities Buying
38 lines (34 loc) · 1.1 KB
/
Securities Buying
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
//Harry has just started investing in stock markets. There are a variety of securities that he can buy, but he is a bit confused about buying the securities because he has a limited amount of money. The prices of the various securities are given for N days and security_value[k] denotes the price of the security on the kth day. There is one more rule, Harry can buy at most k number of securities on the kth day. As mentioned earlier, Harry has a limited amount of money with him to buy these securities and this amount is denoted by z. Can you guide Harry in finding the maximum number of securities that he can buy?
#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
int main()
{
int z;
cin>>z;
int n;
cin>>n;
int a[n];
map<int,int> m;
int sum=0;
for(int i=0;i<n;i++)
{
cin>>a[i];
m[a[i]]=i+1;
}
for(auto a : m)
{
while(a.second>0){
if(z-a.first>=0)
{
z=z-a.first;
sum++;
a.second--;
}
else
break;
}
}
cout<<sum<<endl;
return 0;
}