
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Maximum contiguous sum of subarray in JavaScript
We are required to write a JavaScript function that takes in an array of array of positive and negative integers. Since the array also contains negative elements, the sum of contiguous elements can possibly be negative or positive.
Our function should pick an array of contiguous elements from the array that sums the greatest. Finally, the function should return that array.
For example −
If the input array is −
const arr = [-2, -3, 4, -1, -2, 1, 5, -3];
Then the maximum possible sum is 7 and the output subarray should be −
const output = [4, -1, -2, 1, 5];
Example
Following is the code -
const arr = [-2, -3, 4, -1, -2, 1, 5, -3]; const maximumSubarray = (arr = []) => { let max = -Infinity; let currentSum = 0; let maxStartIndex = 0; let maxEndIndex = arr.length - 1; let currentStartIndex = 0; arr.forEach((currentNumber, currentIndex) => { currentSum += currentNumber; if (max < currentSum) { max = currentSum; maxStartIndex = currentStartIndex; maxEndIndex = currentIndex; } if (currentSum < 0) { currentSum = 0; currentStartIndex = currentIndex + 1; } }); return arr.slice(maxStartIndex, maxEndIndex + 1); }; console.log(maximumSubarray(arr));
Output
Following is the output on console −
[ 4, -1, -2, 1, 5 ]
- Related Articles
- Largest Sum Contiguous Subarray
- Program to find maximum product of contiguous subarray in Python
- Maximum subarray sum in circular array using JavaScript
- C/C++ Program for Largest Sum Contiguous Subarray?
- Contiguous subarray with 0 and 1 in JavaScript
- C++ program to find maximum of each k sized contiguous subarray
- Using Kadane’s algorithm to find maximum sum of subarray in JavaScript
- Program to find sum of contiguous sublist with maximum sum in Python
- Maximum Sum Circular Subarray in C++
- Maximum sum bitonic subarray in C++
- Maximum subarray sum modulo m in C++
- Maximum circular subarray sum in C++\n
- Maximum length of subarray such that sum of the subarray is even in C++
- Finding maximum length of common subarray in JavaScript
- Maximum Subarray Sum with One Deletion in C++

Advertisements