Problem title
Difficulty
Avg time to solve

Contains Duplicate ll
Easy
15 mins
Reverse First K elements of Queue
Easy
10 mins
Count Complete Binary Tree Nodes
Easy
10 mins
Clone a binary tree with random pointers.
Moderate
10 mins
Who Won the Election???
Moderate
30 mins
Missing Vertex In Parallelogram
Easy
10 mins
Sort a half-sorted array
Easy
10 mins
Sort Array
Moderate
15 mins
Largest Submatrix with Equal Number of 0's and 1's
Moderate
15 mins
Minimum Insertions
Moderate
20 mins
5

First Negative In Every Window

Difficulty: MEDIUM
Contributed By
Avg. time to solve
15 min
Success Rate
82%

Problem Statement

You have been given an array of integers 'ARR' of size 'N'. You are also provided with a positive integer 'K'.

Your task is to find the first negative element in every window (contiguous subarray) of length 'K'. If there is no negative element in a window, then print 0 for that window.

For example:
For the given array 'ARR' = [5, -3, 2, 3, -4] and 'K' = 2.
Output = -3 -3 0 -4

We have four windows of length 2 in 'ARR'
[5, -3] having -3 as first negative element.
[-3, 2] having -3 as first negative element.
[2, 3] having no negative element
[2, -4] having -4 as first negative element.
Input Format:
The first line of input contains an integer 'T' representing the number of test cases or queries to be processed. Then the test case follows.

The first line of each test case contains two single space-separated integers 'N' and 'K' representing the size of the array/list and the positive integer denoting the length of the window respectively.

The second line of each test case contains 'N' single space-separated integers representing the array/list elements.
Output Format:
For each test case, print (N - K + 1) single space-separated integers representing the first negative element in each of the windows of length 'K'.

Print output of each test case in a separate line.
Note:
You are not required to print the expected output; it has already been taken care of. Just implement the function. 
Constraints:
1 <= T <= 10
1 <= N <= 5 * 10^4
1 <= K <= N
-10^9 <= ARR[i] <= 10^9

Time Limit: 1 sec
Sample Input 1:
2
5 3
4 0 3 -12 1
3 1
45 12 -6
Sample Output 1:
0 -12 -12
0 0 -6
Explanation For Sample Input 1:
For the first sample test case, we have three windows of length 3 in the first test case

[4, 0, 3] having no negative element.
[0, 3, -12] having -12 as first negative element.
[3, -12, 1] having -12 as the first negative element. 

For the second sample test case, please refer problem statement for the explanation.
Sample Input 2:
2
8 2
4 -7 4 6 7 -11 2 4 
3 2
1 2 3
Sample Output 2:
-7 -7 0 0 -11 -11 0
0 0
Reset Code
Full screen
copy-code
Console