-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP346.java
More file actions
44 lines (29 loc) · 861 Bytes
/
P346.java
File metadata and controls
44 lines (29 loc) · 861 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
package p300_399;
import java.util.Scanner;
import java.util.Stack;
//Accepted
public class P346 {
public static int rangoMax(Stack<Integer> st, int A) {
int rMax = 0;
for (int i = st.size() - 1; i >= 0; i--) {
while (Math.abs(st.get(i)-st.peek()) >= A) {
st.pop();
}
if (st.size()-i>rMax) rMax = st.size()-i;
}
return rMax;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
while (N != 0){
int A = sc.nextInt();
Stack<Integer> stack = new Stack<>();
for (int i = 0; i < N; i++) {
stack.push(sc.nextInt());
}
System.out.println(rangoMax(stack, A));
N = sc.nextInt();
}
}
}