Maximum Sub Array

Maximum Sub Array

The problem is to find the maximum sum of a sub-array of a given integer array. The strategy is to keep track of the sum of current element + previous element and compare it to the current element to find the local max.

Example:
console.log(solution([4,8,2,6,7],[0,1,1,0,0])); = 2
console.log(solution([4,3,2,1,5],[0,1,0,0,0])); = 2

function solution(A, B) {
  let stack = [];
  let survivors = 0;

  for(let i = 0; i < A.length;i++){
    let weight = A[i];
    if (B[i]=== 1) {
      stack.push(weight)
    } else {
      let weightDown = stack.length === 0 ? -1 : stack.pop();
      while(weightDown !== -1 && weightDown < weight)
          weightDown= stack.length === 0  ? -1 : stack.pop();
      if (weightDown === -1) 
        survivors +=1;
       else 
        stack.push(weightDown);      
    }
  }

  return survivors + stack.length;
}

console.log(solution([4,8,2,6,7],[0,1,1,0,0])); 2
console.log(solution([4,3,2,1,5],[0,1,0,0,0])); 2


We create a function called solution with parameter called A and B we create an array called stack and variable survivors that we initialize to 0;

We loop with for of loop for(let i = 0; i < A.length;i++) then we create variable weight equals to A[i]

Inside the for loop we check with if condidition if (B[i]=== 1) and if true we use build in JavaScript method push() to push weight to array stack

Else we create variable weightDown if weightDown is stack.length === 0 we return -1 else we use another build in JavaScript method pop() which removes the last element from an array and we use stack array to pop with stack.pop().

Then we use another loop called while loop that checks weightDown !== -1 && weightDown < weight then again we check if weightDown is stack.length === 0 we return -1 else we pop() from stack array with stack.pop()

Inside the while loop we check if (weightDown === -1) if true we increment survivors by 1
Else we push into stack array with stack.push(weightDown)

In the end we return survivors + stack.length

comments powered by Disqus