We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
You didn't specify an initial value for the reduce function, so the initial value will be the first value of the array. That would mess things up.
Imagine arr = [-10, 2, 4, 5]
maxSeq should be 2 + 4 + 5 = 11
But since you didnt' specify it, so it ended being like this
initial value = -10, then based on logic if (curr >0) -10 + 2, then + 4, then + 5. Instead of 0 +2, then +4 then +5. Total would be 1
Do you see the issue?
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
The Maximum Subarray
You are viewing a single comment's thread. Return to all comments →
You didn't specify an initial value for the reduce function, so the initial value will be the first value of the array. That would mess things up.
Imagine arr = [-10, 2, 4, 5]
maxSeq should be 2 + 4 + 5 = 11
But since you didnt' specify it, so it ended being like this initial value = -10, then based on logic if (curr >0) -10 + 2, then + 4, then + 5. Instead of 0 +2, then +4 then +5. Total would be 1 Do you see the issue?