-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1343.cpp
More file actions
39 lines (32 loc) · 748 Bytes
/
1343.cpp
File metadata and controls
39 lines (32 loc) · 748 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include<iostream>
#include<vector>
int numOfSubarrays(std::vector<int>& arr, int k, int threshold) {
int count = 0;
int sum = 0;
for (int i = 0; i < k; ++i) {
sum += arr[i];
}
int avg = sum / k;
if (avg >= threshold) {
++count;
}
int left = 0;
for (int i = k; i < arr.size(); ++i) {
sum -= arr[left];
++left;
sum += arr[i];
avg = sum / k;
if (avg >= threshold) {
std::cout << avg << ": " << count << "\n";
++count;
}
}
return count;
}
int main() {
std::vector<int> arr = { 2,2,2,2,5,5,5,8 };
int k = 3;
int threshold = 4;
std::cout << numOfSubarrays(arr, k, threshold);
std::cin.get();
}