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

maximum sum array algorithm javascript

function maxSumArr(arr, num) {
    let maxSum = 0;
    let tempSum = 0;
    if(arr.length < num) return null;
    for(let i = 0; i < num; i++) {
       tempSum += arr[i];
    }
    tempSum = maxSum;
    for(let i = num; i < arr.length; i++) {
       tempSum = tempSum - arr[i - num] + arr[i];
       maxSum = Math.max(tempSum, 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 :: click button javascript 
Javascript :: javascript math pi 
Javascript :: How to get tailwindcss intellisense to work with react files 
Javascript :: nodejs wait event loop to finish 
Javascript :: set focus on load javascript 
Javascript :: install react native gifted charts 
Javascript :: javascript class 
Javascript :: jquery value of input 
Javascript :: js form serialize 
Javascript :: alert with sound javascript 
Javascript :: bootstrap prevent dropdown from closing on click 
Javascript :: how to sort array by dates 
Javascript :: new File in js 
Javascript :: vue watch props 
Javascript :: jquery migrate 
Javascript :: javascript get all classes 
Javascript :: jquery format date 
Javascript :: how to go to next line in javascript 
Javascript :: javascript push array into array 
Javascript :: node js return json 
Javascript :: sort by json fied in laravel 
Javascript :: how to stop react app in terminal 
Javascript :: readonly vs disabled 
Javascript :: Install react router in react app 
Javascript :: regex to indentify url 
Javascript :: javascript get current date 
Javascript :: all inputs under div 
Javascript :: angular 9 dockerfile 
Javascript :: run a code after delay js 
Javascript :: logout user firebase 
ADD CONTENT
Topic
Content
Source link
Name
5+1 =