forked from Nikky01/GeekForGeeks
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Stock_buy_and_sell.java
62 lines (54 loc) · 1.1 KB
/
Stock_buy_and_sell.java
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
package com.geek;
import java.util.ArrayList;
import java.util.Scanner;
public class Stock_buy_and_sell {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
for(int i=0;i<t;i++)
{
int n=sc.nextInt();
int a[]=new int[n];
for(int j=0;j<n;j++)
{
a[j]=sc.nextInt();
}
getStockBuySell(a.length,a);
}
}
private static void getStockBuySell(int n, int[] a) {
// TODO Auto-generated method stub
ArrayList<Interval> sol=new ArrayList<Interval>();
if(n==1)
return;
int count=0;
int i=0;
while(i<n-1)
{
while((i<n-1)&&(a[i+1]<=a[i]))
i++;
if(i==n-1)
break;
Interval e=new Interval();
e.buy=i++;
while(i<n&&a[i-1]<=a[i])
i++;
e.sell=i-1;
sol.add(e);
count++;
}
if(count==0)
System.out.println("No Profit");
else
{
for(int j=0;j<count;j++)
System.out.print("("+sol.get(j).buy+" "+sol.get(j).sell+") ");
System.out.println("");
}
}
}
class Interval
{
int buy,sell;
}