Update appNew update is available. Click here to update.

First Negative In Every Window

profile
Contributed by
Omkar Deshmukh
Medium
yellow-spark
0/80
15 mins
82 %
30 upvotes
Amazon

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.
Detailed explanation ( Input/output format, Notes, Images )
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
Full screen
Reset Code
Full screen
Autocomplete
copy-code
Console