From 150bdffeefe8d9b18ab4a2e5f9dd5076e4b47c78 Mon Sep 17 00:00:00 2001 From: Pragya Patel <146708925+Pragya1001@users.noreply.github.com> Date: Sun, 8 Oct 2023 11:00:54 +0530 Subject: [PATCH] Sliding Window Technique.cpp --- Solutions/Sliding Window Technique.cpp | 50 ++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 Solutions/Sliding Window Technique.cpp diff --git a/Solutions/Sliding Window Technique.cpp b/Solutions/Sliding Window Technique.cpp new file mode 100644 index 0000000..7389867 --- /dev/null +++ b/Solutions/Sliding Window Technique.cpp @@ -0,0 +1,50 @@ +#include +using namespace std; + +// Returns maximum sum in a subarray of size k +int max_sum(int arr[], int n, int k) +{ + // n must be greater + if(nn;i++) + { + win_sum+=(arr[i]-arr[i-k]); + max_sum=max(max_sum,win_sum); + } + return max_sum; +} + +int main() +{ + int size; + cout<<"Enter the size of the array:"; + cin>>size; + int arr[size]; + cout<<"Enter the elements of the array : "; + for(int i=0;i>arr[i]; + } + int win_size; + cout<<"Enter the size of the window: "; + cin>>win_size; + cout<<"The max sum is : "<