Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold LeetCode solution in C++

1
class Solution {
2
public:
3
int numOfSubarrays(vector<int> &arr, int k, int threshold) {
4
int i = 0;
5
int j = 0;
6
int sum = 0;
7
int ans = 0;
8
while (j < arr.size()) {
9
sum += arr[j];
10
if (j - i + 1 < k)
11
j++;
12
else if (j - i + 1 == k) {
13
if (sum / k >= threshold) {
14
ans++;
15
}
16
sum -= arr[i];
17
j++;
18
i++;
19
}
20
}
21
return ans;
22
}
23
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0