Maximum sub array sum algo++

I am not getting the desired output, although the code seems to be correct.
Here’s the code


Here is the question
https://hack.codingblocks.com/contests/c/916/1259

hey @ranjanpanda, time limit is 2 sec only. You should do this question using Kadane algorithm which gives O(N) time complexity.

1 Like

Sir my doubt was why is it showing wrong output for even small values of n.
Like for this input.
2
4
1 2 3 4
8
-2 -5 6 -2 -3 1 5 -6
It is showing wrong output.

hey @ranjanpanda, sum should be intialised with 0 instead of a[0]. Also if(sum>currrentSum) should be inside loop. I have made changes to your code. You can check them here https://ide.codingblocks.com/s/107898

1 Like

Thank you so much. But even after trying Kadane algo test case is not getting passed.
Here is my code https://ide.codingblocks.com/s/108338

hey @ranjanpanda, you have maintain the current_sum irrespective to any condition. Your kadanes algo is not correct. I have made changes to your code and indicate them as comment. https://ide.codingblocks.com/s/108358

1 Like

Code passed the test case. Loads of thanks.:relaxed:

hey @ranjanpanda , if your query is resolved. Please mark this doubt as resolved and rate me on the basis of your experience.
rating option will appear when to mark this doubt as resolved

1 Like