Asked 3 months ago
0Comments
7 Views
Given an array of integers, we need to find the first negative number in every window of size k
as the window slides from the start to the end of the array. If there is no negative number in a particular window, we should return 0
for that window.
The sliding window is a technique where we maintain a window of size k
and slide it across the array to perform a specific task for every subset of size k
.
Here In This Problem Statement ! , the task is to identify the first negative number in each window.
Use a Deque to Track Negative Numbers:
Yes, a deque is ideal for this problem because:
Sliding the Window:
k
elements and identify any negative numbers, storing their indices in the deque.0
).Handling Edge Cases:
0
for that window.Let's dry run the code on the array [1, 3, 7, 2, -3, -2, 12, 8, 6, 9]
with a window size k = 3
.
Initial Setup:
[1, 3, 7, 2, -3, -2, 12, 8, 6, 9]
k
): 3
dq
): Empty initially.Processing the First Window (First k
elements):
Element 1
(Index 0
):
1
is not negative, so the deque remains empty.Element 3
(Index 1
):
3
is not negative, so the deque remains empty.Element 7
(Index 2
):
7
is not negative, so the deque remains empty.Output for the First Window:
0
.Next ! Sliding the Window:
Slide to Window [3, 7, 2]
:
2
(Index 3
):
2
is not negative, so the deque remains empty.0
.Slide to Window [7, 2, -3]
:
-3
(Index 4
):
-3
is negative, so we add its index (4
) to the deque.-3
.Slide to Window [2, -3, -2]
:
-2
(Index 5
):
-2
is negative, so we add its index (5
) to the deque.-3
(from the deque).Slide to Window [-3, -2, 12]
:
12
(Index 6
):
12
is not negative, so no changes to the deque.-3
(from the deque).Slide to Window [-2, 12, 8]
:
8
(Index 7
):
8
is not negative, so no changes to the deque.-3
is out of the current window, so we remove it from the deque.-2
.Slide to Window [12, 8, 6]
:
6
(Index 8
):
6
is not negative, so no changes to the deque.-2
is out of the current window, so we remove it from the deque.0
.Slide to Window [8, 6, 9]
:
9
(Index 9
):
9
is not negative, so no changes to the deque.0
.0 0 -3 -3 -3 -2 0 0
Window | Indices | Elements | Deque (dq) State | Output |
---|---|---|---|---|
Window 1 | 0-2 | [1, 3, 7] | [] | 0 |
Window 2 | 1-3 | [3, 7, 2] | [] | 0 |
Window 3 | 2-4 | [7, 2, -3] | [4] | -3 |
Window 4 | 3-5 | [2, -3, -2] | [4, 5] | -3 |
Window 5 | 4-6 | [-3, -2, 12] | [4, 5] | -3 |
Window 6 | 5-7 | [-2, 12, 8] | [5] | -2 |
Window 7 | 6-8 | [12, 8, 6] | [] | 0 |
Window 8 | 7-9 | [8, 6, 9] | [] | 0 |
0
if there are no negatives.#include <deque> // Needed for deque
#include <vector> // Needed for vector
#include <iostream> // Needed for standard I/O
using namespace std;
// Function to find the first negative number in every sliding window of size k
void slidingWindow(vector<int> v, int k) {
deque<int> dq; // Deque to store the indices of negative numbers
int size = v.size();
// Step 1: Process the first window of size k
for (int i = 0; i < k; i++) {
if (v[i] < 0) {
dq.push_back(i); // Store the index of negative number
}
}
// Step 2: Process the rest of the array
for (int i = k; i < size; i++) {
// Output the first negative number of the current window
if (dq.empty()) {
cout << 0 << " ";
} else {
cout << v[dq.front()] << " ";
}
// Remove elements that are out of this window
while (!dq.empty() && dq.front() <= i - k) {
dq.pop_front();
}
// Add the current element if it's negative
if (v[i] < 0) {
dq.push_back(i);
}
}
// Output the first negative number of the last window
if (dq.empty()) {
cout << 0 << " ";
} else {
cout << v[dq.front()] << " ";
}
cout << endl; // Move to the next line after output
}
int main() {
vector<int> v = {1, 3, 7, 2, -3, -2, 12, 8, 6, 9}; // Example array
int k = 3; // Window size
slidingWindow(v, k); // Call the function
return 0;
}
k
elements into the deque.O(n)
- We process each element once, and each element is added and removed from the deque at most once.O(k)
- The deque holds at most k
elements (indices of the negative numbers within the window).This approach effectively handles the sliding window problem while ensuring clarity and ease of understanding, especially for those new to data structures and algorithms.
Share