Number of Sub-arrays of Size K and Average ≥ Threshold
Problem Statement
You are given an integer array arr, a window size k and a number threshold.
Look at every contiguous block of exactly k elements. Count how many of these blocks have an average that is at least threshold.
An integer array arr, and integers k and threshold.
An integer: the number of size-k subarrays whose average is ≥ threshold.
The blocks [2,5,5], [5,5,5] and [5,5,8] have averages 4, 5 and 6. Every other size-3 block has an average below 4.
The first 6 blocks of size 3 all have an average above 5. The last two, [7,5,2] and [5,2,3], do not.
- • 1 ≤ arr.length ≤ 10^5
- • 1 ≤ arr[i] ≤ 10^4
- • 1 ≤ k ≤ arr.length
- • 0 ≤ threshold ≤ 10^4
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Recompute each block's sum and compare with threshold · k
Slide one running sum; count windows with sum ≥ threshold · k
For every start index, add up the k elements of that block from scratch. If the sum is at least threshold · k, count it.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Sliding Window translate into production architectures and low-level memory mechanics.