You are viewing a single comment's thread. Return to all comments →
JS
function maxSubarray(array) { const getMaxSubsum = array => { let maxSum = -Infinity; array.reduce((sum, value) => { sum += value; maxSum = Math.max(maxSum, sum); return (sum > 0) ? sum : 0; }, 0); return maxSum; }; const getMaxSubsequence = array => { array.sort((a, b) => a - b); return (array.at(-1) < 0) ? array.at(-1) : array.filter(value => value >= 0).reduce((sum, value) => sum + value); }; return [getMaxSubsum(array), getMaxSubsequence(array)]; }
Seems like cookies are disabled on this browser, please enable them to open this website
The Maximum Subarray
You are viewing a single comment's thread. Return to all comments →
JS