-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBuySell.java
More file actions
65 lines (50 loc) · 773 Bytes
/
BuySell.java
File metadata and controls
65 lines (50 loc) · 773 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
53
54
55
56
57
58
59
60
61
62
63
64
65
import java.util.*;
class BuySell
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
System.out.println("Enter the size of the array");
int size=in.nextInt();
int arr[]=new int[size];
for(int k=0;k<size;k++)
{
System.out.println("Enter the stock price at day"+k);
arr[k]=in.nextInt();
}
int buyDay=0;
int sellDay=0;
int k=0;
int m=k+1;
int nextBuyDay=m;
while(m<size)
{
int big=arr[k];
buyDay=k;
int flag=0;
while(m<size)
{
if(arr[m]>big)
{
big=arr[m];
sellDay=m;
}
else
{
nextBuyDay=m;
flag=1;
break;
}
m++;
k++;
}
if(buyDay!=sellDay)
System.out.println("buy at day "+buyDay+" and sell at day "+sellDay);
if(flag==1)
{
k=nextBuyDay;
m=k+1;
}
}//while
}
}