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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution { public: intnumOfSubarrays(vector<int>& arr, int k, int threshold){ longlong sum = 1ll * k * threshold; longlong window = 0; for(int i = 0; i < k; i++) { window += arr[i]; } int res = window >= sum; for(int i = k; i < arr.size(); i++) { window += arr[i] - arr[i-k]; res += window >= sum; } return res; } };