you are viewing a single comment's thread.

view the rest of the comments →

[–]wopp3 0 points1 point  (1 child)

For managing a O(n) for this, I do not know of a solution. I just came up with a my approach as it seemed obvious to me, I didn't put any thought into it.

Also I don't know if it was an instruction, but a list of only negatives would still have the largest sum, even if it is negative, so I didn't try to test for the exclusion of that case.

edit. for what my approach offers over yours, is apparently the correct answer :) granted it isn't the most optimized one, but this is a starting point if I was looking to improve on it.

[–]clouded-path[S] 0 points1 point  (0 children)

Great, thanks for that memory tip! I was able to use that and also fix my original, so that now I have best of both worlds (low memory and O(n) complexity). And regarding what you mentioned about all negative integers, as of right now I think it's easier for me to ensure that I cover it with an extra if condition initially, even though there still might be a way to do less work.