-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstockbuysell.java
More file actions
74 lines (46 loc) · 1.03 KB
/
stockbuysell.java
File metadata and controls
74 lines (46 loc) · 1.03 KB
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
import java.util.ArrayList;
class Interval
{
int buy, sell;
}
class StockBuySell
{
void stockBuySell(int price[], int n)
{
if (n == 1)
return;
int count = 0;
ArrayList<Interval> sol = new ArrayList<Interval>();
int i = 0;
while (i < n - 1)
{
while ((i < n - 1) && (price[i + 1] <= price[i]))
i++;
if (i == n - 1)
break;
Interval e = new Interval();
e.buy = i++;
while ((i < n) && (price[i] >= price[i - 1]))
i++;
e.sell = i-1;
sol.add(e);
count++;
}
if (count == 0)
System.out.println("TXM"
+ "will make profit");
else
for (int j = 0; j < count; j++)
System.out.println("Buy on day: " + sol.get(j).buy
+" " + "Sell on day : " + sol.get(j).sell);
return;
}
public static void main(String args[])
{
StockBuySell stock = new StockBuySell();
int price[] = {100, 180, 260, 310, 40, 535, 695};
int n = price.length;
stock.stockBuySell(price, n);
}
}
/*---SD*/