Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Maximum sum subarray of size k.cpp #340

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
}