Imagine you have a list of numbers, and you want to look at them through a small "window" that shows only k numbers at a time. You start at the left, count how many of those k numbers are negative, then slide the window one step to the right and count again. You keep doing this until the window reaches the end of the list.
A subarray is just a small continuous chunk of the array. A subarray "of size k" means a chunk that holds exactly k numbers sitting next to each other.
You are given:
arrkYour job is to return a list. Each value in that list is the count of negative numbers inside one window of size k, going from left to right.
arr = [4, -4, 5, -1, 4] and k = 3.
We slide a window of 3 numbers across the array:
[4, -4, 5] → only -4 is negative → count is 1[-4, 5, -1] → -4 and -1 are negative → count is 2[5, -1, 4] → only -1 is negative → count is 1So the answer is [1, 2, 1].
Notice the array has 5 numbers, and with a window of size 3 we get exactly 3 windows. The pattern is simple: number of windows = (length of array) − k + 1. Here that is 5 − 3 + 1 = 3.
arr = [1, -2, 3, -5] and k = 1.
When k = 1, each window is just a single number. So for every number we simply ask: "Is this negative?" If yes, the count is 1; if no, the count is 0.
[1] → not negative → 0[-2] → negative → 1[3] → not negative → 0[-5] → negative → 1So the answer is [0, 1, 0, 1].
The simplest idea: for each starting position, look at the next k numbers and count the negatives. This works, but it re-checks the same numbers again and again.
A smarter idea is the sliding window trick. Instead of recounting from scratch each time, you keep a running count. When the window slides one step:
This way you only do a tiny bit of work per slide, which is much faster for long arrays.
The function you need to write returns a vector<int> (a list of integers), where each integer is the negative-count for one window:
class Solution {
public:
vector<int> negativeWindow(vector<int> &a
};