Kadane's algo for max sum in subarry

Here we are talking about positive sum, so what about when all elements are negative ,it will not work for those cases ,the max sum which we will get is zero.

hello @learninggood

u can modify its implementation a bit , to make it work for evry cases.

int max_so_far      = array[0];
int max_ending_here = 0;


for (int i = 0; i < size; i++)
{
    max_ending_here =max_ending_here + array[i];
    max_so_far      = max(max_ending_here, max_so_far);
    max_ending_here = max(max_ending_here, 0);

}

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.