Kadane's algorithm subarray sum

suppose there is an array with all the negative elements in it. If we apply kadane’s algo, the max sum will be 0,but instead it should be the some negative number?

@rockstarpkm,
Yes, that is a backdrop of Kadane’s Algo, but you can have a round about, multiply all elements by -1, then apply kadane’s also, and multiply the final answer by -1.

but we won’t know the input array. It’s more like an edge case , so how should we resolve it?

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.

@rockstarpkm,
You can simply check in O(n) that if all elements are negative beforehand, then carry on accordingly.

1 Like