Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold
Real-world version: Windows That Reach the Average BarThe statement here is our own: the same inputs, outputs and technique.
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.
def count_qualifying_windows(readings: list[int], span: int, bar: int) -> int:public int countQualifyingWindows(int[] readings, int span, int bar)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 10 test cases: 10 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's