http://ide.codingblocks.com/#/s/15083
this is my code. i used kadanes algorithm for finding maximum sum.
the code is working fine but on hackerblock it shows run-error.
Maximum subarray problem doubt
Your code will not work if array contains only negative elements because sum will be negative for this case and before saving it to max you are updating c to zero so first check for max and then update c to zero
Ur modified code -->>
http://ide.codingblocks.com/#/s/16168
1 Like
i can’t view your modified code ,can you please repost it.
#include
using namespace std;
int main() {
int a[2000],m,n,k,s,c,i;
cin>>n;
k=0;
while(k<n)
{
cin>>m;
for(i=0; i<m; i++)
cin>>a[i];
s=0;
c=0;
for(i=0; i<m; i++)
{
c=c+a[i];
s=max(c,s);
if(c<0)
c=0;
}
cout<<s<<endl;
k++;
}
return 0;
}
1 Like