--- title: Greatest subsequential sum id: 5a23c84252665b21eecc7e84 challengeType: 5 --- ## Description
Given a sequence of integers, find a continuous subsequence which maximizes the sum of its elements, that is, the elements of no other single subsequence add up to a value larger than this one. An empty subsequence is considered to have the sum of \( 0 \); thus if all elements are negative, the result must be the empty sequence.
## Instructions
## Tests
```yml tests: - text: maximumSubsequence should be a function. testString: assert(typeof maximumSubsequence=='function','maximumSubsequence should be a function.'); - text: maximumSubsequence([1,2,-1,3,10,-10]) should return an array. testString: assert(Array.isArray(maximumSubsequence([1,2,-1,3,10,-10])),'maximumSubsequence([1,2,-1,3,10,-10]) should return an array.'); - text: maximumSubsequence([1,2,-1,3,10,-10]) should return [ 1, 2, -1, 3, 10 ]. testString: assert.deepEqual(maximumSubsequence([1,2,-1,3,10,-10]), [ 1, 2, -1, 3, 10 ],'maximumSubsequence([1,2,-1,3,10,-10]) should return [ 1, 2, -1, 3, 10 ].'); - text: maximumSubsequence([0, 8, 10, -2, -4, -1, -5, -3]) should return [ 0, 8, 10 ]. testString: assert.deepEqual(maximumSubsequence([0, 8, 10, -2, -4, -1, -5, -3]), [ 0, 8, 10 ],'maximumSubsequence([0, 8, 10, -2, -4, -1, -5, -3]) should return [ 0, 8, 10 ].'); - text: maximumSubsequence([9, 9, -10, 1]) should return [ 9, 9 ]. testString: assert.deepEqual(maximumSubsequence([9, 9, -10, 1]), [ 9, 9 ],'maximumSubsequence([9, 9, -10, 1]) should return [ 9, 9 ].'); - text: maximumSubsequence([7, 1, -5, -3, -8, 1] should return [ 7, 1 ]. testString: assert.deepEqual(maximumSubsequence([7, 1, -5, -3, -8, 1]), [ 7, 1 ],'maximumSubsequence([7, 1, -5, -3, -8, 1] should return [ 7, 1 ].'); - text: maximumSubsequence([-3, 6, -1, 4, -4, -6]) should return [ 6, -1, 4 ]. testString: assert.deepEqual(maximumSubsequence([-3, 6, -1, 4, -4, -6]), [ 6, -1, 4 ],'maximumSubsequence([-3, 6, -1, 4, -4, -6]) should return [ 6, -1, 4 ].'); - text: maximumSubsequence([-1, -2, 3, 5, 6, -2, -1, 4, -4, 2, -1]) should return [ 3, 5, 6, -2, -1, 4 ]. testString: assert.deepEqual(maximumSubsequence([-1, -2, 3, 5, 6, -2, -1, 4, -4, 2, -1]), [ 3, 5, 6, -2, -1, 4 ],'maximumSubsequence([-1, -2, 3, 5, 6, -2, -1, 4, -4, 2, -1]) should return [ 3, 5, 6, -2, -1, 4 ].'); ```
## Challenge Seed
```js function maximumSubsequence (population) { // Good luck! } ```
## Solution
```js function maximumSubsequence(population) { function sumValues(arr) { var result = 0; for (var i = 0, len = arr.length; i < len; i++) { result += arr[i]; } return result; } var greatest; var maxValue = 0; var subsequence = []; for (var i = 0, len = population.length; i < len; i++) { for (var j = i; j <= len; j++) { var subsequence = population.slice(i, j); var value = sumValues(subsequence); if (value > maxValue) { maxValue = value; greatest = subsequence; }; } } return greatest; } ```