Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold (LeetCode 1343): Solution and Explanation
Count the windows of length k whose average is at least threshold. Multiply instead of dividing, so each window only needs sum >= k * threshold, and keep the sum up to date with one add and one subtract per step.
Intuition
Averages bring fractions and rounding questions, but sum / k >= threshold holds exactly when sum >= k * threshold. With a fixed k, the test becomes a plain integer comparison against one target.
Two neighbouring windows of size k share k - 1 elements. Moving one step right only changes the element that enters and the element that leaves.
So one running sum, nudged by arr[i] - arr[i - k] at every step, gives every window's sum in O(1).
Approach: Fixed-size sliding window
- Set
target = k * threshold. - Sum the first
kelements and startcountat 1 if that sum reachestarget, else 0. - For
ifromkton - 1, updatewindow += arr[i] - arr[i - k]. - If
window >= target, incrementcount. - Return
count.
Step-by-step example
arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4
- target = 3 * 4 = 12.
- First window [2,2,2]: sum 6 < 12, count = 0.
- Slide: [2,2,2] sum 6, then [2,2,5] sum 9, both short.
- [2,5,5] sum 12 >= 12, count = 1. [5,5,5] sum 15, count = 2. [5,5,8] sum 18, count = 3.
- Answer: 3.
Code: Python, Java, C++
1class Solution:
2 def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int:
3 target = k * threshold
4 window = sum(arr[:k])
5 count = 1 if window >= target else 0
6 for i in range(k, len(arr)):
7 window += arr[i] - arr[i - k]
8 if window >= target:
9 count += 1
10 return count1class Solution {
2 public int numOfSubarrays(int[] arr, int k, int threshold) {
3 int target = k * threshold;
4 int window = 0;
5 for (int i = 0; i < k; i++) {
6 window += arr[i];
7 }
8 int count = window >= target ? 1 : 0;
9 for (int i = k; i < arr.length; i++) {
10 window += arr[i] - arr[i - k];
11 if (window >= target) {
12 count++;
13 }
14 }
15 return count;
16 }
17}1class Solution {
2public:
3 int numOfSubarrays(vector<int>& arr, int k, int threshold) {
4 int target = k * threshold;
5 int window = 0;
6 for (int i = 0; i < k; i++) {
7 window += arr[i];
8 }
9 int count = window >= target ? 1 : 0;
10 for (int i = k; i < (int)arr.size(); i++) {
11 window += arr[i] - arr[i - k];
12 if (window >= target) {
13 count++;
14 }
15 }
16 return count;
17 }
18};Time and space complexity
O(n)
Each element enters the window once and leaves it once.
O(1)
Only the running sum, the target and the count.
Other approaches
Recompute every window (time O(n * k), space O(1))
Sum each window of k from scratch. Simple, but costs O(n * k), which is too slow when both are large.
Prefix sums (time O(n), space O(n))
Build prefix[i] and read each window as prefix[i + k] - prefix[i]. Same O(n) time, with an O(n) array.
Common mistakes
- Dividing with integer division:
sum / k >= thresholdtruncates and can count a window whose true average is below the threshold. Comparesum >= k * thresholdinstead. - Forgetting to check the first window before the slide loop starts.
- Subtracting
arr[i - k + 1]instead ofarr[i - k], which leaves the window one element too short. - Recomputing the window sum every step, which is quadratic for large
k.
Frequently asked questions
Can k * threshold overflow?
No. k is at most 10^5 and threshold at most 10^4, so the product is at most 10^9, which fits in a 32-bit int. Window sums are bounded the same way.
Why compare with k * threshold instead of computing the average?
It avoids floating point and integer division entirely. Both sides are integers, so the comparison is exact.
What if k equals the array length?
There is exactly one window, the whole array, so the answer is 1 or 0 depending on whether its sum reaches the target.
What is the time and space complexity?
O(n) time and O(1) extra space.