I Implemented the Kadane’s Max Sub array problem in javascript but seems i end up always getting 0 in the console even though there exists higher numbers( I understand that it does what it’s doing because of for loop from 0 - size where size = subarray size).
-
So how do i implement the algorithm correctly?
-
Does it also work for all positive integer array?
You’re passing n=3 as an argument while your array has length 6. I changed your algorithm to use
length:and it gives 17.
Yes, it will then give sum of all elements in the array.
If you want maximal subsequence of length 3, use
the best is 4+6+7, while 4+6+7+1+4 is not allowed.