-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStock_Buy-Sell_Problem.cpp
More file actions
50 lines (35 loc) · 912 Bytes
/
Copy pathStock_Buy-Sell_Problem.cpp
File metadata and controls
50 lines (35 loc) · 912 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
#include <bits/stdc++.h>
using namespace std;
void stockBuySell(int prices[] , int n)
{
if(n == 1)
{
cout<<"Cannot buy or sell."<<endl;
return;
}
int i = 0 , j = 1;
int low = prices[i] , high = prices[j];
while((j<n) && (j<n))
{
//while cost is lower than next day -->(BUY)
while((i < n) && (prices[i] > prices[i+1]))
i++;
cout<<"BUY @ "<<prices[i]<<" and ";
//while cost is higher than next day --> (SELL)
while((j < n) && (prices[j+1] > prices[j]))
j++;
cout<<"SELL @ "<<prices[j]<<endl;
//Cannot buy further hence break
if(j == n-1)
break;
i = j+1;
j = j+1;
}
}
int main()
{
int prices[] = {100 , 180 , 260 , 310 , 40 , 535 , 695};
int n = sizeof(prices) / sizeof(prices[0]);
stockBuySell(prices , n);
return 0;
}