i cant get the right result of the contiguous subarray of arr with the maximal sum of items

Νίκος Γλαβίνας

so i found a solution for this mathematical problem which is this one

function getMaxSubSum(arr) {
    let maxSum = 0;
    let partialSum = 0;
  
    for (let item of arr) { // for each item of arr
      partialSum += item; // add it to partialSum
      maxSum = Math.max(maxSum, partialSum); // remember the maximum
      if (partialSum < 0) partialSum = 0; // zero if negative
    }
  
    return maxSum;
  }

  alert ( getMaxSubSum([1,-2,3,9,-9,6]) )

BUT i want to achieve it with another way and im trying this code

function kadane () {
    arr = [1,-2,3,9,-9,6]
    let  maxSub = maxGlobal = arr[0]
    for (i=3 ; i<arr.length-1; i++ ) {
     maxSub = Math.max(arr[i], maxSub + arr[i])
       
    if (maxSub > maxGlobal) {
        maxSub = maxGlobal

       
    }

    
    
}
return  maxSub

}
alert (kadane())

does anyone know what im doing wrong?

RenaudC5

Your solution were really close !

Here you inverted maxSub and maxGlobal in the if section.

Also, I don't know why but your for loop where starting at 3 instead of 1.

Here is your working example

function kadane(arr) {
  let maxSub = arr[0]
  let maxGlobal = arr[0]
  for (i = 1; i < arr.length; i++) {
    maxSub = Math.max(arr[i], maxSub + arr[i])
    if (maxSub > maxGlobal) {
      maxGlobal = maxSub
    }
  }
  return maxGlobal
}

const arr = [1, -2, 3, 9, -9, 6]
alert(kadane(arr))


A little more...

Also, note that you could also have checked the maximum of 2 numbers consecutively.

Example using Array#reduce

function kadane(arr) {
  return arr.reduce((acc, curr, index) => {
    if(index === 0) return curr > 0 ? curr : 0
    else {
      const sum = curr + arr[index-1]
      return sum > acc ? sum : acc
    }
  }, 0)
}

console.log(kadane([1, -2, 3, 9, -9, 6]))
console.log(kadane([-1, -2, -3, -4, -5, -6]))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Debugging: Largest Sum Contiguous Subarray

I cant get the result of y

Finding Maximal-Sum Positive Integer Subarray

Largest sum contiguous subarray (Interview Question)

largest sum of contiguous subarray No Larger than k

Maximum sum of any contiguous subarray of the array

JavaScript algorithm question: get the find the contiguous subarray which has the largest sum from an array

How to get sum of n subarray items in N arrays, PHP Laravel

Maximum sum in a contiguous subarray of a given 2D array

How to check if sum of some contiguous subarray is equal to N?

Cant get the result when I use multi threading with Python

How would I get the right result in this query?

Maximal subarray with length constraint

Cant get datetime formatting right

How can I find a subarray with some contiguous elements randomly in C?

What is contiguous subarray

Number of distinct contiguous subarray

Max sum of i*arr[i]

How do I sum subarray elements and group by subarray value?

Get sum of subarray column for indexed rows

contiguous subarray within an array (containing at least one number) which has the largest sum

how to find the length of the longest contiguous subarray whose sum is divisible by a given number

Calculate the maximal even cost subarray

I cant seem to get the amount of decimals right using setprecision c++

If i replace "arr[index]" with "arr[items]" i get an array with the values of [ 2, 0, 3, 4 ], can someone explain what is happening

What do I have to change in my code to get the right result?

What I should change in this Delphi code, to get the right result?

Java cant get right values for vars

cant get the wordpress search bar on the right position