Update appNew update is available. Click here to update.
sidenav-btnClose
Topic list
First Negative In Every Window
MEDIUM
15 mins
13 upvotes
Stacks & Queues
Arrays
Topics (Covered in this problem)
Problem solved
Badge
Skill meter
Stacks & Queues
-
-
Arrays
-
-
Other topics
Problem solved
Badge
Skill meter
Strings
-
-
Matrices (2D Arrays)
-
-
Linked List
-
-
Sorting
-
-
Binary Search
-
-
Trees
-
-
Graph
-
-
Dynamic Programming
-
-
Greedy
-
-
Tries
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
userLevel
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

First Negative In Every Window

Contributed by
Omkar Deshmukh
Medium
yellow-spark
0/80
Avg time to solve 15 mins
Success Rate 82 %
Share
13 upvotes

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, Constraints, Images )
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
Auto
copy-code
Console