
- 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
Finding the sub array that has maximum sum JavaScript
We are required to write a JavaScript function that takes in an array of Numbers. The array of numbers can contain both positive as well as negative numbers.
The purpose of our function is to find the sub array from the array (of any length), whose elements when summed gives the maximum sum. Then the function should return the sum of the elements of that subarray.
For example −
If the input array is −
const arr = [-2,1,-3,4,-1,2,1,-5,4];
Then the output should be −
const output = 6
because, [4,-1,2,1] has the largest sum of 6.
Example
const arr = [-2,1,-3,4,-1,2,1,-5,4]; const maxSubArray = (arr = []) => { let sum = arr[0], max = arr[0]; for (let i = 1; i < arr.length; ++i){ sum = Math.max(sum + arr[i], arr[i]), max = Math.max(max, sum); }; return max; }; console.log(maxSubArray(arr));
Output
And the output in the console will be −
6
- Related Articles
- Finding the maximum in a nested array - JavaScript
- Finding the sum of unique array values - JavaScript
- JavaScript Finding the third maximum number in an array
- Finding the third maximum number within an array in JavaScript
- Maximum size of sub-array that satisfies the given condition in C++
- Finding sum of alternative elements of the array in JavaScript
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST in C++
- Maximum size of sub-array that satisfies the given condition in C++ program
- Finding the maximum square sub-matrix with all equal elements in C++
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST in C++ program
- Finding sum of a range in an array JavaScript
- Maximum sum Bi-tonic Sub-sequence in C++
- Maximum subarray sum in circular array using JavaScript
- Maximum sum possible for a sub-sequence such that no two elements appear at a distance < K in the array in C++
- Finding sum of every nth element of array in JavaScript

Advertisements