Kadane's Algorithm for Maximum SubArray sum

In the Video of maximum SubArray Sum using kadane’s algorithm code is shown but it doesn’t work if only negative element is present in the array.
I think this code works fine in which only if block is interchanged and maximumSum is initialized with INT_MIN.

code link : https://ide.codingblocks.com/s/78212

Hey ritik
What exactly your question is? Do you want me to check your code? Are you facing any problem while submitting it somewhere?

@priyanka123 Thank you for reply. From my side i checked and it works fine. So Please can you check with your testcases?

Ritik
Your code is okay when all numbers are negative.
consider {-2, -3, 4, -1, -2, 1, 5, -3}
In this case your algo is giving answer as 4
However answer is 7 by using (4,-1,-2,1,5)
Try doing a dry run for this case and modify the algo accordingly. If you get struck anywhere, feel free to pass on your code, I will tell you the mistakes …!

@priyanka123 {-2, -3, 4, -1, -2, 1, 5, -3}
In this case my algo is giving answer as 7. I think you forgot to change size of array.

code Link : https://ide.codingblocks.com/s/78212

Oops Sorry I thought you are calculating the size, didn’t notice I had to change it
Yess Ritik, your code is working fine as per test cases that I tried.

1 Like

@priyanka123 Thanks a lot Priyanka Daryani.

1 Like

can you tell me what is wrong in this code as i am gettinfg the wrong ans as i submit the code on hacker blocks
here is my code- https://ide.codingblocks.com/s/93754