Show problems as
Classic problem #1343MediumArraySliding Window

Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold

Real-world version: Windows That Reach the Average Bar
Tracked in this browser only

A rooftop sensor writes one integer reading per minute into the list readings. An analyst inspects every window of exactly span consecutive readings and keeps the window when the mean of its readings is at least bar.

Two windows are different when they start at different minutes, so a list of n readings has n - span + 1 windows to inspect.

Return how many of those windows the analyst keeps.

Examples

Example 1

Input
readings = [7, 2, 9, 4, 6, 3, 8], span = 3, bar = 6
Output
2

The five windows total 18, 15, 19, 13 and 17, so their means are 6, 5, 6.33..., 4.33... and 5.66.... Only the first and the third reach a mean of 6.

Example 2

Input
readings = [8, 1, 1, 8], span = 2, bar = 4
Output
2

The three windows total 9, 2 and 9, giving means 4.5, 1 and 4.5. The first and the last reach the bar of 4.

Example 3

Input
readings = [4, 4, 4, 4, 4], span = 5, bar = 3
Output
1

The span equals the number of readings, so there is one window; its mean is 4, which reaches the bar.

Constraints

  • 1 <= readings.length <= 10^5
  • 1 <= readings[i] <= 10^4
  • 1 <= span <= readings.length
  • 0 <= bar <= 10^4

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def count_qualifying_windows(readings: list[int], span: int, bar: int) -> int:
Java
public int countQualifyingWindows(int[] readings, int span, int bar)