-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGFG.java
More file actions
112 lines (88 loc) · 2.92 KB
/
Copy pathGFG.java
File metadata and controls
112 lines (88 loc) · 2.92 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import java.util.*;
//https://www.geeksforgeeks.org/stock-buy-sell/
public class GFG {
public GFG() {
super();
}
Scanner s = new Scanner(System.in);
public static void main(String[] args) {
GFG gfg = new GFG();
Scanner s = new Scanner(System.in);
//System.out.println("please enter the number");
int Testcases = Integer.parseInt(s.nextLine());
int i;
for(i=0;i<Testcases;i++)
{
gfg.action();
}
s.close();
}
public void action()
{
int n = Integer.parseInt(s.nextLine());
//int() A = new int(n);
String all_input = s.nextLine();
String[] integerStrings = all_input.split(" ");
// Splits each spaced integer into a String array.
int[] A = new int[n];
// Creates the integer array.
for (int i = 0; i < A.length; i++){
A[i] = Integer.parseInt(integerStrings[i]);
//Parses the integer for each string.
}
int LP=0,HP=0;
int j=1;
while(j<=(A.length-1))
{
if(A[j]>A[HP])
{
HP=j;
j=j+1;
//System.out.print("HP IS first"+HP);
//System.out.print("LP IS first "+LP);
if(HP==(A.length-1))
{
if(HP-LP==0)
{
//System.out.print("came here 2");
if(HP==A.length-1){
System.out.println("No Profit");
}
}
else{
System.out.print("("+LP+" "+HP+")" );
}
}
}
else if(A[j]==A[HP])
{
HP=j;
if(A[LP]==A[HP])
{
LP=j;
}
j=j+1;
if(HP-LP==0)
{
//System.out.print("came here 2");
if(HP==A.length-1){
System.out.println("No Profit");
}
}
}
else
{
if(HP-LP>0)
{
//System.out.print("came here 1");
System.out.print("("+LP+" "+HP+")" );
//System.out.print("HP IS "+HP);
//System.out.print("LP IS "+LP);
}
HP=j;
LP=j;
j=j+1;
}
}
}
}