Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold
Fixed-Size Sliding Window
Asked at 7 companies
4 recent
- Amazon
- Microsoft
- Bloomberg
- Goldman Sachs
- Meta
Community-reported · source · updated 2026-11-18. ● asked in the last 6 months.
Go step by step: try it yourself before the video and the visual. Progress saves on this device.
0 / 130 pointsSolved (Medium) 0/40Reviews 0/50Mastered 0/25Practice steps 0/15
Step 1
Know these before you start
Practise each one here, or tick what you already know
Watch the concept video if the pattern is new to you.
Step 2
Try it on paper
5–10 minutes, no code yet
ProblemProblem statementGiven an array of integers arr and two integers k and threshold, return the number of sub-arrays of size k and average greater than or equal to threshold.ReadHide
Problem Statement
Given an array of integers arr and two integers k and threshold, return the number of sub-arrays of size k and average greater than or equal to threshold.
Added 2026-11-25 from Srujan's LeetCode Recent AC list (photo) — not previously in the repo.
Input / Output
Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4
Output: 3
Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold).
Input: arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5
Output: 6
Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers.
Code it on LeetCode
There's no editor for this one here yet, so solve it on LeetCode, with its own tests. Then tick My code passed below, and it joins your reviews. With your LeetCode account linked, that happens on its own.
An error or a Wrong Answer there? What each one means.
Step 6
Get it accepted
All test cases pass
Step 7
Review it so it sticks
Re-solve from memory after
The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.
Your notes
Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.
Solve it yourself before you look
Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.
Hidden: approach · solution code
Approach
Pattern Recognition
Fixed-size sliding window — every sub-array has the same length k, so the next window is just the previous one with one number dropped on the left and one added on the right. Keep a running sum instead of re-adding k numbers every time, and compare the sum against k * threshold (no floating-point division needed, since sum / k >= threshold is the same as sum >= k * threshold).
Approach
Brute Force — Re-Sum Every Window, O(n * k)
count = 0
for i in range(len(arr) - k + 1):
if sum(arr[i:i + k]) / k >= threshold:
count += 1
return count
Correct, but it adds up k numbers for every one of the n - k + 1 windows.
Optimized — Sliding Window, O(n)
window = sum(arr[:k])
count = 1 if window >= k * threshold else 0
for i in range(k, len(arr)):
window += arr[i] - arr[i - k] # slide: add the new right edge, drop the old left edge
if window >= k * threshold:
count += 1
return count
Example: [2,2,2,2,5,5,5,8], k = 3, threshold = 4 — window [2,2,2] sums to 6 (avg 2, no), slide to [2,2,5] (avg 3, no), slide to [2,5,5] (avg 4, yes), [5,5,5] (avg 5, yes), [5,5,8] (avg 6, yes) → 3.
Complexity
| Approach | Time | Space | Notes |
|---|---|---|---|
| Re-sum each window | O(n * k) | O(1) | Adds k numbers per window, over and over |
| Sliding window | O(n) | O(1) | One addition and one subtraction per step |
Solution code
class SolutionBruteForce:
"""O(n * k) time | O(1) space — re-sum every window of size k."""
def numOfSubarrays(self, arr: list[int], k: int, threshold: int) -> int:
count = 0
for i in range(len(arr) - k + 1):
if sum(arr[i:i + k]) / k >= threshold:
count += 1
return count
class Solution:
"""O(n) time | O(1) space — sliding window; compare sum against k * threshold."""
def numOfSubarrays(self, arr: list[int], k: int, threshold: int) -> int:
window = sum(arr[:k])
count = 1 if window >= k * threshold else 0
for i in range(k, len(arr)):
window += arr[i] - arr[i - k]
if window >= k * threshold:
count += 1
return countOther ways to solve it
- Community solutions on LeetCode ↗ for many more versions, in many languages. Read them after you've solved it.
Revising this one?
- →Solve it from memory before peeking
- →Say the pattern out loud, not just the code
- →Explain it like you would to a friend
- →Try it again in a different language