why in kadane’s algorithm we initialise maximum sum so far as 0 since if there is a case like where all elements in an array are negative than it will give answer as 0 but the answer should be max negative value
Related to kadane's algorithm
Hi priyal
you can initialize maximum so far as INT_MIN
You can try this as well
int max_so_far = INT_MIN;
int max_ending_here = array[0];
for (int i = 1; i < size; i++) {
max_ending_here = max(max_ending_here + array[i], array[i]);
max_so_far = max(max_ending_here, max_so_far);
}
cout<<max_so_far<<endl;
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.