#include<bits/stdc++.h>
using namespace std;
int MaximumSum(int a[], int n, int sz)
{
if(n < sz)
return -1;
int maxsum = 0, currentsum = 0;
for(int i=0; i<sz; i++)
currentsum += a[i];
maxsum = currentsum;
for(int i=sz; i<n; i++)
{
currentsum = currentsum - a[i-sz] + a[i];
maxsum = max(maxsum, currentsum);
}
return maxsum;
}
int main()
{
int n, sz;
cout << "Enter Array Size :: ";
cin >> n;
cout << "\nEnter Window Size :: ";
cin >> sz;
int a[n]; /// Declar a array
cout << "\nEnter Array Element :: ";
for(int i=0; i<n; i++) /// Enter Array Element 10 40 10 30 20
cin >> a[i];
cout << "\n";
int mx = MaximumSum(a, n, sz);
if(mx != -1)
cout << "Maximum Sum :: " << mx << "\n"; /// 40 10 30 = 80 Maximum sum
else
cout << "Maximum Summation Not Found." << "\n";
return 0;
}
Comments
Post a Comment