Skip to content

Commit 66832aa

Browse files
authored
Create Stock_Buy_Sell.java
1 parent 431735a commit 66832aa

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

Arrays/Stock_Buy_Sell.java

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import java.util.*;
2+
import java.lang.*;
3+
import java.io.*;
4+
class GFG{
5+
public static void main (String[] args){
6+
//code
7+
Scanner s = new Scanner(System.in);
8+
int t = s.nextInt();
9+
10+
while(t-- > 0){
11+
int n = s.nextInt();
12+
int arr[] = new int[n];
13+
14+
for(int i = 0 ; i < n ; i++){
15+
arr[i] = s.nextInt();
16+
}
17+
18+
List<Integer> buy = new ArrayList<>();
19+
List<Integer> sell = new ArrayList<>();
20+
21+
int stocks = 0;
22+
int i = 0;
23+
while(i <= n-2){ //we
24+
while(i < n-1 && arr[i] >= arr[i+1]){
25+
i++;
26+
}
27+
28+
if(i == n-1) break;
29+
30+
buy.add(i++);
31+
32+
while(i < n && arr[i] >= arr[i-1]){
33+
i++;
34+
}
35+
36+
sell.add(i-1);
37+
38+
stocks++;
39+
}
40+
41+
if(stocks == 0) System.out.println("No Profit");
42+
else{
43+
for(i = 0 ; i < buy.size() ; i++){
44+
System.out.print("(" + buy.get(i) + " " + sell.get(i) + ") ");
45+
}
46+
System.out.println();
47+
}
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)