#1343
Medium Algorithms Number of sub arrays of size k and average greater than or equal to threshold
Array Sliding Window
72.3% acceptance
Feb 25, 2026
1830
111
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.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn num_of_subarrays(arr: Vec<i32>, k: i32, threshold: i32) -> i32 {
let k = k as usize;
let min_sum = threshold * k as i32;
let mut sum: i32 = arr[..k].iter().sum();
let mut count = if sum >= min_sum { 1 } else { 0 };
for i in k..arr.len() {
sum += arr[i] - arr[i - k];
if sum >= min_sum { count += 1; }
}
count
}
}