Introduction
The First Negative Number in Every Window of Size K problem involves finding the first negative number present in every contiguous subarray (window) of size k.
Given an array arr[] 0f size n and an integer k, the task is to print the first negative number in every window of size k. If a window does not contain a negative number, print 0.
This is an important Sliding Window problem that helps in understanding window processing, queue-based optimization, and efficient traversal techniques.
Example
Input: arr[] = [2, -1, -3, 4, 5, -2, 6]k = 3 Output: [-1, -1, -3, -2, -2] Explanation: Window [2, -1, -3] → First Negative = -1 Window [-1, -3, 4] → First Negative = -1 Window [-3, 4, 5] → First Negative = -3
Window [4, 5, -2] → First Negative = -2
Window [5, -2, 6] → First Negative = -2
Input: arr[] = [1, 2, 3, 4] k = 2 Output: [0, 0, 0] Explanation:
No negative numbers are present in any window.
Constraints
1 <= n <= 10^51 <= k <= n
-10^9 <= arr[i] <= 10^9
Approach 1 : Brute Force
Explanation
The simplest way to solve this problem is:
- Generate every window of size k
- Traverse each window
- Find the first negative number
If no negative number exists in a window, print 0.
This approach is simple but repeatedly traversing every window increases time complexity.
Steps
- Traverse all possible windows.
- For every window:
- traverse all k elements
- Find the first negative number.
- If no negative number exists:
- print 0
Dry Run
Input Array: [2, -1, -3, 4, 5, -2, 6]k = 3 Window [2, -1, -3]: First Negative = -1 Window [-1, -3, 4]: First Negative = -1 Window [-3, 4, 5]: First Negative = -3 Window [4, 5, -2]: First Negative = -2
Window [5, -2, 6]:
First Negative = -2
Final Result:
[-1, -1, -3, -2, -2]
Brute Force Code
Complexity Analysis
Time Complexity: O(n * k)Explanation: Every window is traversed separately.
Space Complexity: O(1) Explanation: No extra data structures are used apart from result storage.
Approach 2 : Optimized Solution (Sliding Window + Queue)
Explanation
Instead of traversing every window again and again, we can use:
- Sliding Window Technique
- Queue to store negative numbers
The idea is:
- Store indices of negative numbers
- Remove elements that go outside the window
- The front of the queue always gives the first negative number
This avoids repeated traversals and improves efficiency.
Steps
- Create a queue to store indices of negative numbers.
- Traverse the array.
- Add negative elements into the queue.
- Remove elements outside the current window.
- Once window size becomes k:
- front of queue gives first negative number
- if queue is empty, print 0
Dry Run
Input Array: [2, -1, -3, 4, 5, -2, 6]k = 3 Window [2, -1, -3]: Negative elements = [-1, -3] First Negative = -1
Slide Window:
Remove 2
Add 4
Window [-1, -3, 4]
First Negative = -1
Slide Window Again:
Remove -1
Add 5
Window [-3, 4, 5]
First Negative = -3
Slide Window Again:
Remove -3
Add -2
Window [4, 5, -2]
First Negative = -2
Final Result:
[-1, -1, -3, -2, -2]
Optimized Code