Created
December 26, 2020 07:45
-
-
Save akshat-fsociety/c1b8864fd14501070dcdd03d2881735f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void main(String[] args) { | |
FastReader s = new FastReader(); | |
int n = s.nextInt(); | |
int k = s.nextInt(); | |
int a[] = new int[n]; | |
for (int i = 0; i < n; i++) | |
a[i] = s.nextInt(); //accepting array | |
Queue<Integer> q = new LinkedList<>(); | |
for (int i = 0; i < k; i++) { | |
if (a[i] < 0) | |
q.offer(i); // Adding the index of -ve element | |
} // Running this loop till the size of window | |
for (int i = k; i < n; i++) { | |
if (!q.isEmpty()) | |
System.out.print(a[q.peek()] + " "); // if q is not empty display the -ve term | |
else | |
System.out.print(0 + " "); | |
while (!q.isEmpty() && q.peek() < i - k + 1) { // Out from window range | |
q.poll(); | |
} | |
if (a[i] < 0) | |
q.offer(i); | |
} | |
if (!q.isEmpty()) | |
System.out.print(a[q.peek()] + " "); | |
else | |
System.out.print(0 + " "); | |
System.out.println(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment