Nodejs Utililty Methods Array Max Sub Array

List of utility methods to do Array Max Sub Array

Description

The list of methods to do Array Max Sub Array are organized into topic(s).

Method

maxSubArray()
'use strict';
Array.prototype.maxSubArray = function() {
  let maxToIndex = null;
  let currentMax = null;
  let maxElement = null;
  this.forEach((element) => {
    maxToIndex = Math.max(0, maxToIndex + element);
    currentMax = Math.max(currentMax, maxToIndex);
    maxElement = maxElement == null || element > maxElement ? element : maxElement;
...
maxSubArray()
'use strict';
Array.prototype.maxSubArray = Array.prototype.maxSubArray || function () {
  let maxToIndex = null;
  let currentMax = null;
  let maxElement = null;
  this.forEach(element => {
    maxToIndex = Math.max(0, maxToIndex + element);
    currentMax = Math.max(currentMax, maxToIndex);
    maxElement = maxElement === null || element > maxElement ? element : maxElement;
...
maxSubArrayNonContigious()
Array.prototype.maxSubArrayNonContigious = function() {
  let maxElement = null;
  let sum = null;
  this.forEach((element) => {
    maxElement = maxElement == null || element > maxElement ? element : maxElement;
    sum += element > 0 ? element : 0;
  });
  return sum > 0 ? sum : maxElement;
};
...
maxSubArrayNonContigious()
Array.prototype.maxSubArrayNonContigious = Array.prototype.maxSubArrayNonContigious || function () {
  let maxElement = null;
  let sum = null;
  this.forEach(element => {
    maxElement = maxElement === null || element > maxElement ? element : maxElement;
    sum += element > 0 ? element : 0;
  });
  return sum > 0 ? sum : maxElement;
};
...