You are given an array of n integers and a window length k. Slide a window of k consecutive elements from left to right, producing n - k + 1 windows. For each window, report the first negative value that appears in it (scanning the window from left to right). If a window contains no negative value, report 0 for that window.
Because 0 is not negative, it is never itself a valid “first negative” answer, so using 0 as the “no negative present” marker is unambiguous.
Line 1: two integers n and k separated by a space.
Line 2: n space-separated integers, the array values.
A single line with n - k + 1 values separated by single spaces: for each window in left-to-right order, its first negative value, or 0 if the window has no negative value.
Example 1
Input
8 3 12 -1 -7 8 -15 30 16 28
Expected
-1 -1 -7 -15 -15 0
Explanation
Windows: [12,-1,-7]->-1, [-1,-7,8]->-1, [-7,8,-15]->-7, [8,-15,30]->-15, [-15,30,16]->-15, [30,16,28] has no negative ->0, giving -1 -1 -7 -15 -15 0.
Example 2
Input
5 2 5 6 7 8 9
Expected
0 0 0 0
Explanation
No window of length 2 contains a negative value, so every answer is 0, giving 0 0 0 0.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →