Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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.
Example 1:
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).
Example 2:
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.
Constraints:
1 <= arr.length <= 1051 <= arr[i] <= 1041 <= k <= arr.length0 <= threshold <= 104Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Sliding Window
[2,2,2,2,5,5,5,8] 3 4
[11,13,17,23,29,31,7,5,2,3] 3 5
k-radius-subarray-averages)count-subarrays-with-median-k)apply-operations-to-make-all-array-elements-equal-to-zero)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1343: Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold
class Solution {
public int numOfSubarrays(int[] arr, int k, int threshold) {
threshold *= k;
int s = 0;
for (int i = 0; i < k; ++i) {
s += arr[i];
}
int ans = s >= threshold ? 1 : 0;
for (int i = k; i < arr.length; ++i) {
s += arr[i] - arr[i - k];
ans += s >= threshold ? 1 : 0;
}
return ans;
}
}
// Accepted solution for LeetCode #1343: Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold
func numOfSubarrays(arr []int, k int, threshold int) (ans int) {
threshold *= k
s := 0
for _, x := range arr[:k] {
s += x
}
if s >= threshold {
ans++
}
for i := k; i < len(arr); i++ {
s += arr[i] - arr[i-k]
if s >= threshold {
ans++
}
}
return
}
# Accepted solution for LeetCode #1343: Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold
class Solution:
def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int:
threshold *= k
s = sum(arr[:k])
ans = int(s >= threshold)
for i in range(k, len(arr)):
s += arr[i] - arr[i - k]
ans += int(s >= threshold)
return ans
// Accepted solution for LeetCode #1343: Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold
struct Solution;
impl Solution {
fn num_of_subarrays(arr: Vec<i32>, k: i32, threshold: i32) -> i32 {
let n = arr.len();
let mut prefix = vec![0; n + 1];
let sum = threshold * k;
let k = k as usize;
let mut res = 0;
for i in 0..n {
prefix[i + 1] = prefix[i] + arr[i];
}
for r in k..=n {
let l = r - k;
if prefix[r] - prefix[l] >= sum {
res += 1;
}
}
res
}
}
#[test]
fn test() {
let arr = vec![2, 2, 2, 2, 5, 5, 5, 8];
let k = 3;
let threshold = 4;
let res = 3;
assert_eq!(Solution::num_of_subarrays(arr, k, threshold), res);
let arr = vec![1, 1, 1, 1, 1];
let k = 1;
let threshold = 0;
let res = 5;
assert_eq!(Solution::num_of_subarrays(arr, k, threshold), res);
let arr = vec![11, 13, 17, 23, 29, 31, 7, 5, 2, 3];
let k = 3;
let threshold = 5;
let res = 6;
assert_eq!(Solution::num_of_subarrays(arr, k, threshold), res);
let arr = vec![7, 7, 7, 7, 7, 7, 7];
let k = 7;
let threshold = 7;
let res = 1;
assert_eq!(Solution::num_of_subarrays(arr, k, threshold), res);
let arr = vec![4, 4, 4, 4];
let k = 4;
let threshold = 1;
let res = 1;
assert_eq!(Solution::num_of_subarrays(arr, k, threshold), res);
}
// Accepted solution for LeetCode #1343: Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold
function numOfSubarrays(arr: number[], k: number, threshold: number): number {
threshold *= k;
let s = arr.slice(0, k).reduce((acc, cur) => acc + cur, 0);
let ans = s >= threshold ? 1 : 0;
for (let i = k; i < arr.length; ++i) {
s += arr[i] - arr[i - k];
ans += s >= threshold ? 1 : 0;
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
For each starting index, scan the next k elements to compute the window aggregate. There are n−k+1 starting positions, each requiring O(k) work, giving O(n × k) total. No extra space since we recompute from scratch each time.
The window expands and contracts as we scan left to right. Each element enters the window at most once and leaves at most once, giving 2n total operations = O(n). Space depends on what we track inside the window (a hash map of at most k distinct elements, or O(1) for a fixed-size window).
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Using `if` instead of `while` leaves the window invalid for multiple iterations.
Usually fails on: Over-limit windows stay invalid and produce wrong lengths/counts.
Fix: Shrink in a `while` loop until the invariant is valid again.