The Maximum Subarray

  • + 0 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?