-
-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathMaximum sum subarray of size k.cpp
More file actions
43 lines (36 loc) · 891 Bytes
/
Copy pathMaximum sum subarray of size k.cpp
File metadata and controls
43 lines (36 loc) · 891 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
42
43
// This is the code for finding maximum sum of a subarray of size k using sliding
// window approach. Time complexity: O(n)
#include<bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(false);cin.tie(0);
int findmaxsum(vector<int>& arr, int k)
{
int sum=0, maxsum=INT_MIN;
int i=0, j=0;
int n= arr.size();
while(j<n)
{
sum+= arr[j]; //add element to sum
if(j-i+1 <k) //check if window size reached
{
j++;
}
else if((j-i+1)==k)
{
maxsum= max(maxsum, sum);
sum-=arr[i];
i++;
j++;
}
}
return maxsum;
}
int main()
{
fast
vector<int> arr{1, 4, 2, 10, 23, 3, 1, 0, 20};
int k=4;
// output will be 39
cout<<"maximum sum of a subarray of size "<<k<<" is "<< findmaxsum(arr, k);
return 0;
}