-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuy_two_chocolates.cpp
More file actions
52 lines (50 loc) · 993 Bytes
/
buy_two_chocolates.cpp
File metadata and controls
52 lines (50 loc) · 993 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
45
46
47
48
49
50
51
52
#include <stdc++.h>
using namespace std;
int buyChoco(vector<int> &prices, int money)
{
int m = INT_MIN;
int ok = 0;
for (int i = 0; i < prices.size(); i++)
{
for (int j = prices.size() - 1; j > i; j--)
{
if (prices.at(i) + prices.at(j) < money)
{
int temp = money - (prices.at(i) + prices.at(j));
if (temp >= m)
{
m = temp;
ok = 1;
}
}
else if (prices.at(i) + prices.at(j) == money)
{
ok = 2;
}
}
}
if (ok == 1)
{
return m;
}
else if (ok == 2)
{
return 0;
}
return money;
}
int main()
{
vector<int> v;
int a, x, m;
cin >> a;
for (int i = 0; i < a; i++)
{
cin >> x;
v.push_back(x);
}
cout << "Enter money : ";
cin >> m;
cout << buyChoco(v, m);
return 0;
}