Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold (LeetCode 1343): Solution and Explanation

Medium
Array
Sliding window

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

  1. Set target = k * threshold.
  2. Sum the first k elements and start count at 1 if that sum reaches target, else 0.
  3. For i from k to n - 1, update window += arr[i] - arr[i - k].
  4. If window >= target, increment count.
  5. Return count.

Step-by-step example

arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4
  1. target = 3 * 4 = 12.
  2. First window [2,2,2]: sum 6 < 12, count = 0.
  3. Slide: [2,2,2] sum 6, then [2,2,5] sum 9, both short.
  4. [2,5,5] sum 12 >= 12, count = 1. [5,5,5] sum 15, count = 2. [5,5,8] sum 18, count = 3.
  5. 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 count
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

Each element enters the window once and leaves it once.

Space

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 >= threshold truncates and can count a window whose true average is below the threshold. Compare sum >= k * threshold instead.
  • Forgetting to check the first window before the slide loop starts.
  • Subtracting arr[i - k + 1] instead of arr[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.