-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGFG_ARR_MaximumSumSubarrayOfSizeK.cpp
48 lines (44 loc) · 1.17 KB
/
GFG_ARR_MaximumSumSubarrayOfSizeK.cpp
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
42
43
44
45
46
47
48
/*
https://practice.geeksforgeeks.org/problems/max-sum-subarray-of-size-k5313/1
*/
// } Driver Code Ends
class Solution{
public:
int maximumSumSubarray(int K, vector<int> &Arr , int N){
// given K<=N already
int k_sum = 0, cw_sum; // sum of first K size window
for (int i=0; i<K; i++){
k_sum += Arr[i];
}
// compute sum by removing the first element of the last window
// and adding the last element of the current window
cw_sum = k_sum;
for (int i=K; i<N; i++){
cw_sum += Arr[i] - Arr[i-K];
// k_sum = max(k_sum, cw_sum); //0.03
if(k_sum<cw_sum) //0.02
k_sum = cw_sum;
}
return k_sum;
}
};
// { Driver Code Starts.
int main()
{
int t;
cin>>t;
while(t--)
{
int N,K;
cin >> N >> K;;
vector<int>Arr;
for(int i=0;i<N;++i){
int x;
cin>>x;
Arr.push_back(x);
}
Solution ob;
cout << ob.maximumSumSubarray(K,Arr,N) << endl;
}
return 0;
} // } Driver Code Ends