You are given an array of integers (both positive and negative). Find the continous sequence with the largest sum. Return the sum.
##Example
Input: {2, -8, 3, -2, 4, -10}
Output: 5 (i.e, {3, -2, 4})
##Powerd by CodeWarrior
You are given an array of integers (both positive and negative). Find the continous sequence with the largest sum. Return the sum.
##Example
Input: {2, -8, 3, -2, 4, -10}
Output: 5 (i.e, {3, -2, 4})
##Powerd by CodeWarrior
module.exports = function (seq) { | |
var maxsum = 0; | |
for (var i = 0; i < seq.length; i++) { | |
var sum = 0; | |
for (var j = i; j < seq.length; j++) { | |
sum += seq[j]; | |
if(sum > maxsum) { | |
maxsum = sum; | |
} | |
}; | |
}; | |
return maxsum; | |
} |
{ | |
"id": 9, | |
"name": "continous sequence sum", | |
"level": "basic", | |
"author": "CareerCup" | |
} |
var func = require("./"); | |
var expect = require("expect.js") | |
describe("func", function () { | |
it("should return largest sum in sequence", function() { | |
expect(func([2, -8, 3, -2, 4, -10])).to.equal(5); | |
}); | |
}); |