-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathFirstNegativeOfEveryWindowK.java
More file actions
50 lines (40 loc) · 984 Bytes
/
FirstNegativeOfEveryWindowK.java
File metadata and controls
50 lines (40 loc) · 984 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
package lecture9a14;
import java.util.Scanner;
import lecture9a13.queue;
public class FirstNegativeOfEveryWindowK {
static Scanner a = new Scanner(System.in);
public static void main(String[] args) throws Exception {
int n = a.nextInt();
int[] arr = new int [n];
int i=0;
while(i<n) {
arr[i] = a.nextInt();
i++;
}
int k= a.nextInt();
firstnegative(arr, k);
}
public static void firstnegative(int [] arr, int k) throws Exception {
queue q = new queue(100);
for(int i=0 ; i<k ; i++)
if(arr[i]<0)
q.enqueue(i);
for(int i=k ; i<arr.length ; i++) {
//print
if(q.isEmpty())
System.out.println(0);
else
System.out.println(arr[q.getFront()]);
//remove
if( !q.isEmpty()&&q.getFront()<=i-k)
q.dequeue();
//insert
if(arr[i]<0)
q.enqueue(i);
}
if(q.isEmpty())
System.out.println(0);
else
System.out.println(arr[q.getFront()]);
}
}