Opening the reading…
Opening the reading…
SLIDING WINDOW › FIXED SIZE SLIDING-WINDOW
Open — the attempt gate is not wired up yet
This editorial is meant to unlock after you have run the problem at least once, with the worked solution behind one further deliberate click. That needs per-learner unlock state nothing stores today, so for now the whole article is open.
Try it yourself first →Every candidate has exactly k elements, so its average is at least threshold exactly when its sum is at least k multiplied by threshold. Comparing sums avoids division, floating point values, and any ambiguity around averages that are not whole numbers.
The first length-k subarray requires k additions. After that, the next window differs in only two places: it loses the element at the old left edge and gains the next element at the right edge. Keep the current sum, remove arr[i - k], add arr[i], and each new window is ready in constant time instead of being summed from scratch.
| MEASURE | BOUND | WHY |
|---|---|---|
| Time | O(n) | The first window uses k additions, and each of the remaining n - k windows uses one addition, one subtraction, and one comparison. No element is included in a window sum more than once during initialization and once more during its removal, so the total work is linear in n. |
| Space | O(1) extra | Only sum, count, target, and loop variables are stored; the returned integer is not working memory. The bound stays O(1) for every input shape because the algorithm never allocates a structure proportional to the array. |
#include <vector>
using namespace std;
class Solution {
public:
int numOfSubarrays(vector<int>& arr, int k, int threshold) {
int n = arr.size();
int target = k * threshold;
int sum = 0;
for (int i = 0; i < k; ++i) {
sum += arr[i];
}
int count = (sum >= target) ? 1 : 0;
for (int i = k; i < n; ++i) {
sum += arr[i] - arr[i - k];
if (sum >= target) {
++count;
}
}
return count;
}
};The expression sum += arr[i] - arr[i - k] is the central line. At the start of an iteration, sum belongs to the window ending at i - 1. Removing arr[i - k] discards its old first element, and adding arr[i] creates the window ending at i. The count check comes after both updates, so it evaluates the new window rather than the previous one.
A prefix-sum array can answer any length-k window sum as prefix[i + k] - prefix[i]. This is still O(n) time, but it stores n + 1 cumulative sums instead of reusing one running sum. It is a reasonable choice when the same array needs many later range-sum queries, but for this one-pass task it buys no speed and costs O(n) extra space.
#include <vector>
using namespace std;
class Solution {
public:
int numOfSubarrays(vector<int>& arr, int k, int threshold) {
int n = arr.size();
int target = k * threshold;
vector<int> prefix(n + 1, 0);
for (int i = 0; i < n; ++i) {
prefix[i + 1] = prefix[i] + arr[i];
}
int count = 0;
for (int start = 0; start + k <= n; ++start) {
int sum = prefix[start + k] - prefix[start];
if (sum >= target) {
++count;
}
}
return count;
}
};