Sir in this algorithm we cannot use for all negeative number like
-6 ,-1 ,-3 ,-2
here if we apply kadanes algo we will get it as 0
but anser is -1 isnt it?
Kadane's algorithm doubt
if you change the order of if statements then the kadane’s algorithm would work even for negative numbers.
Code
Sir but can we do both at one time i mean same code for all negative and same code for all positive and same code for mix sign?
the code i have provided you works for every case.
where is the code???
i tried and made some code can you just check all the test cases or is it failing in any test cases?
#include
#include
using namespace std;
int main()
{
int cumlative_sum,max_sum,a[50];
cout<<“Enter the number of elelemnts in array=”<<endl;
int array_size;
cin>>array_size;
for(int i=0;i<array_size;i++)
{
cin>>a[i];
}
cumlative_sum=a[0];
max_sum=INT_MIN;
for(int i=1;i<array_size;i++)
{
if(max_sum<cumlative_sum)
{
max_sum=cumlative_sum;
}
if(a[i]<0 and cumlative_sum<0)
{
cumlative_sum=a[i];
}
else if(cumlative_sum<0 and a[i]>=0)
{
cumlative_sum=a[i];
}
else
{
cumlative_sum=cumlative_sum+a[i];
}
}
if(max_sum<cumlative_sum)
{
max_sum=cumlative_sum;
}
cout<<“Max sum=”<<max_sum;
return 0;
}
i have shared code in my fist reply and if you want to share your code, please share it using ide.codingblocks.com