diff --git a/DSA/Basic-DSA-Questions/Sliding Window/Maximum sum subarray of size k.cpp b/DSA/Basic-DSA-Questions/Sliding Window/Maximum sum subarray of size k.cpp new file mode 100644 index 00000000..155e8b91 --- /dev/null +++ b/DSA/Basic-DSA-Questions/Sliding Window/Maximum sum subarray of size k.cpp @@ -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 +using namespace std; +#define fast ios::sync_with_stdio(false);cin.tie(0); + +int findmaxsum(vector& arr, int k) +{ + int sum=0, maxsum=INT_MIN; + int i=0, j=0; + int n= arr.size(); + + while(j 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 "<