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

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

PREVIOUS NEXT
Code Example
Javascript :: mongoose encrypt password 
Javascript :: javasciprt set cookie 
Javascript :: react native map 
Javascript :: vscode add shortcut to run in terminal 
Javascript :: django ajax redirect to a view on success 
Javascript :: how to target checkbox in jquery 
Javascript :: sanitize html in javascript 
Javascript :: js overflowy 
Javascript :: firebase functions add to database 
Javascript :: ajax file upload input 
Javascript :: how to check if local storage is available 
Javascript :: node.js 8 has been deprecated. firebase functions 
Javascript :: append javascript example 
Javascript :: difference between normal function and arrow function 
Javascript :: best reactjs course on udemy 
Javascript :: run for loop every second js 
Javascript :: import npm dotenv package 
Javascript :: javascript insertion sort 
Javascript :: expo react navigation 
Javascript :: log error line node.js 
Javascript :: javascript goto page 
Javascript :: how to copy a javascript array 
Javascript :: jquery get value of element 
Javascript :: get textarea value jquery 
Javascript :: UnhandledPromiseRejectionWarning: TypeError: Converting circular structure to JSON 
Javascript :: animate change background color angular 
Javascript :: how to make an event listener only work once 
Javascript :: python minify json 
Javascript :: how to check if email already exists in database using javascript 
Javascript :: js combine two arrays 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =