Kadanes algorithm

what if all the nos in array are negative?? then it will always update it to zero and our answer will be zero which is not even in the array

hello @himanshu24

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.