#include<bits/stdc++.h>
using namespace std;
int maxSum(int a[], int n, int k)
{
int maxSum[n];
maxSum[0] = a[0];
int curr_max = a[0];
for (int i = 1; i < n; i++)
{
curr_max = max(a[i], curr_max+a[i]);
maxSum[i] = curr_max;
}
int sum = 0;
for (int i = 0; i < k; i++)
sum += a[i];
int result = sum;
for (int i = k; i < n; i++)
{
sum = sum + a[i]-a[i-k];
result = max(result, sum);
result = max(result, sum + maxSum[i-k]);
}
return result;
}
int main()
{
int a[100],k=5,i,n; cin>>n;
for(i=0;i<n;i++)
cin>>a[i];
cout << maxSum(a, n, k);
return 0;
}
using namespace std;
int maxSum(int a[], int n, int k)
{
int maxSum[n];
maxSum[0] = a[0];
int curr_max = a[0];
for (int i = 1; i < n; i++)
{
curr_max = max(a[i], curr_max+a[i]);
maxSum[i] = curr_max;
}
int sum = 0;
for (int i = 0; i < k; i++)
sum += a[i];
int result = sum;
for (int i = k; i < n; i++)
{
sum = sum + a[i]-a[i-k];
result = max(result, sum);
result = max(result, sum + maxSum[i-k]);
}
return result;
}
int main()
{
int a[100],k=5,i,n; cin>>n;
for(i=0;i<n;i++)
cin>>a[i];
cout << maxSum(a, n, k);
return 0;
}
Ar11 75% evaluation plz help
ReplyDelete100% working program
ReplyDelete####################
#include
int maxSum(int a[], int n, int k)
{ int i;
for(i=0;i<n;i++)
{
k=k+a[i];
}
printf("%d",k);
}
int main() {
int sum=0;
int i,n,arr[n];
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
maxSum(arr,n,sum);
return 0;
}
only 80%
DeleteThis comment has been removed by the author.
ReplyDelete