Problem 319
First Negative in Every Window
Easy · 10 pts · Array, Queue, Sliding Window
Problem
A window of size K slides over the array from left to right. For each of the N - K + 1 windows, print the first (leftmost) negative number inside that window, or 0 if the window contains no negative number. A queue of the positions of negative numbers lets you answer every window in O(N) total.
Input
The first line contains N and K. The second line contains N integers.
Output
Print N - K + 1 values, separated by spaces.
Constraints
1 <= K <= N <= 100000; -100000 <= A[i] <= 100000
Example input
5 2 -8 2 3 -6 10
Example output
-8 0 -6 -6
Explanation
With K = 2 in -8 2 3 -6 10, the windows are [-8 2], [2 3], [3 -6] and [-6 10]. Their first negatives are -8, none (0), -6 and -6.