Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

maximum sum subarray javascript

const maxSubArray = (nums) => {
    // initiate two variable, maxSum for total max, sum for current max
    let maxSum = -Infinity
    let currentSum = 0
    // iterate through the nums, store sub-problems result
    for(let i = 0; i < nums.length; i++){ 
        //cumulating answers to the top
        
        //compare currentSum add current number 
        //with current number and store the maximum value
        currentSum = Math.max(nums[i], currentSum + nums[i])
        
        //compare maxSum with currentSum and store the greater value
        maxSum = Math.max(currentSum, maxSum)
        
    }
    return maxSum
}
Comment

largest sum contiguous subarray javascript

Using kadane's algorithm

const maxSubArray = (nums) => {
    // initiate two variable, maxSum for total max, sum for current max
    let maxSum = -Infinity
    let currentSum = 0
    // iterate through the nums, store sub-problems result
    for(let i = 0; i < nums.length; i++){ 
        //cumulating answers to the top
        
        //compare currentSum add current number 
        //with current number and store the maximum value
        currentSum = Math.max(nums[i], currentSum + nums[i])
        
        //compare maxSum with currentSum and store the greater value
        maxSum = Math.max(currentSum, maxSum)
        
    }
    return maxSum
}
Comment

js how to get max sub array sum

let arr = [-11, 15, -9, -2, -3, -5, 8];

const checkArr = (/** @type {number[]} */ arr) => arr.every((/** @type {number} */ elem) => elem < 0);

/**
 * @param {number[]} arr
 */
function getMaxSubSum(arr) {
  let maxSum = 0;
  let currentSum = 0;

  if (checkArr(arr)) return 0;

  for (let i = 0; i < arr.length; ++i) {
    currentSum = Math.max(arr[i], currentSum + arr[i]);

    maxSum = Math.max(currentSum, maxSum);
  }

  return maxSum;
}

console.log(getMaxSubSum(arr));
Comment

PREVIOUS NEXT
Code Example
Javascript :: js number to str 
Javascript :: javascript best way to loop through array 
Javascript :: javascript date array 
Javascript :: javascript ajax get 
Javascript :: How to update one mongoose db 
Javascript :: javascript days until end of month 
Javascript :: counting sheep 
Javascript :: how to run cypress test 
Javascript :: Using fetch to upload files 
Javascript :: prevent form submit html javascript jquery 
Javascript :: jquery onclick multiple buttons 
Javascript :: GET method firebase realtime database react 
Javascript :: strict mode 
Javascript :: how to check if email already exists in database using javascript 
Javascript :: date compare in js 
Javascript :: update to node 15.11 
Javascript :: how to give custom name to collection in mongoose 
Javascript :: react typescript set type 
Javascript :: jquery ajax refresh 
Javascript :: luxon plus 
Javascript :: how copy url of page to clipboard javascript 
Javascript :: map method js 
Javascript :: javascript inheritence 
Javascript :: ajax timer 
Javascript :: react router switch 
Javascript :: react-native make android apk 
Javascript :: javascript mutation observer 
Javascript :: Regular Expressions: Extract Matches 
Javascript :: sort array of numbers js 
Javascript :: indefOf 
ADD CONTENT
Topic
Content
Source link
Name
4+5 =