-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday36.cpp
More file actions
41 lines (32 loc) · 990 Bytes
/
day36.cpp
File metadata and controls
41 lines (32 loc) · 990 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
39
40
41
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int T;
cin >> T; // Number of test cases
while (T--) {
int n, k;
cin >> n; // Number of chests
vector<int> gold(n);
// Input the gold array
for (int i = 0; i < n; i++) {
cin >> gold[i];
}
cin >> k; // Size of the sliding window
// Sliding window to find the maximum sum of k consecutive chests
long long current_sum = 0, max_sum = 0;
// Compute the sum of the first window of size k
for (int i = 0; i < k; i++) {
current_sum += gold[i];
}
max_sum = current_sum;
// Slide the window across the array
for (int i = k; i < n; i++) {
current_sum += gold[i] - gold[i - k];
max_sum = max(max_sum, current_sum);
}
cout << max_sum << endl; // Output the result for this test case
}
return 0;
}