for the inout
4
4 5 -7 1
output should be 9 as 4+5 =9
but giving 5 as output
Wrong output for the above explained programme
4 5 -7 1 is the array
@roushanraj395
Hello Roshan,
yeah output should be 9,can u please share ur code so that i can check ur mistake
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n =4;
int a[n]={4,5,-7,1};
int currsum=0, maxsum=0,left,right;
int cumsum[n]={0};
cumsum[0]=a[0];
for (int i = 1; i < n; i++)
{
cumsum[i]=cumsum[i-1]+a[i];
}
for (int i = 0; i < n; i++)
{
for(int j=i;j<n;j++){
currsum=0;
currsum=cumsum[j]-cumsum[i-1];
if(currsum>maxsum){
maxsum=currsum;
left = i;
right =j;
}
}
}
cout<<maxsum<<endl;
for (int i = left; i <= right; i++)
{
cout<<a[i]<<",";
}
return 0;
}
@roushanraj395
brother pls save ur code on coding blocks ide and then share the generated link here.
@roushanraj395
here is ur code link.
basically u missed one corner case (i,e when i=0)
in that case ur currsum must be equal to cumsum[j]
something like
