Skip to content

Instantly share code, notes, and snippets.

@Rafe
Created April 2, 2013 16:02
Show Gist options
  • Save Rafe/5293405 to your computer and use it in GitHub Desktop.
Save Rafe/5293405 to your computer and use it in GitHub Desktop.
Solving continous sequence sum problem from http://code-warrior.herokuapp.com.

Continous sequence sum

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);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment