- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# JavaScript program for Size of the Subarray with Maximum Sum

JavaScript program for the Size of the Subarray with Maximum Sum is a common problem in the field of programming, especially in web development. The problem statement involves finding the contiguous subarray within a given one-dimensional array of integers which has the largest sum. This is also known as the maximum subarray problem. Solving this problem can be extremely useful in a variety of applications, such as financial analysis, stock market prediction, and signal processing.

In this article, we will see the algorithm and the implementation of the size of the subarray with maximum sum using JavaScript. We will begin by discussing the problem in detail and then proceed to develop a step-by-step solution using JavaScript programming language. So let's get started!

## Problem Statement

Given an array of integers, we have to find the length of the subarray having the maximum sum.

For example, let's say we have an array of integers: [1, -2, 1, 1, -2, 1], the maximum subarray would be [1, 1], with a sum of 2. We can find the length of this subarray by subtracting the starting index from the ending index and adding 1. In this case, the starting index is 0 and the ending index is 1, so the length of the subarray is 2.

Another example would be an array of all negative integers: [-2, -5, -8, -3, -1, -7]. In this case, the maximum subarray would be [-1] with a sum of -1. Since all the elements are negative, the subarray with the smallest absolute value will have the maximum sum. Therefore, the length of the subarray is -1.

It is important to note that there can be multiple maximum subarrays, each with the same sum. However, we only need to find one of them.

## Algorithm

### STEP 1

We start by initializing four variables- 'maxSum' to '-Infinity', 'currentSum' to '0', 'start' to '0', and end to '0'. We will use 'maxSum' to keep track of the maximum sum we have seen so far, 'currentSum' to calculate the sum of the subarray we are currently iterating over, 'start' to keep track of the start index of the subarray, and 'end' to keep track of the end index of the subarray.

### STEP 2

We then loop through the array using a 'for' loop. For each element in the array, we add it to 'currentSum'. If 'currentSum' is greater than 'maxSum', we update 'maxSum' to 'currentSum' and set 'end' to the current index.

### STEP 3

Next, we use a while loop to check if 'currentSum' is less than '0'. If it is, we subtract the value at 'start' from 'currentSum' and increment 'start' by 1. This ensures that we always have a contiguous subset of the array.

### STEP 4

Finally, we check if 'currentSum' is equal to 'maxSum' and the size of the current subarray is greater than the previous subarray. If it is, we update 'end' to the current index.

### STEP 5

The time complexity of this algorithm is O(n), and the space complexity is O(1), which is optimal for this problem.

## Example

The below JavaScript program aims to solve the problem of finding the contiguous subarray with the largest sum in an array of integers using two pointers, start and end. The algorithm initializes the maximum sum to negative infinity, the current sum to zero, and the start and end indices to zero. It adds each element to the current sum and updates the maximum sum and end index if the current sum is greater than the maximum sum. It removes elements from the beginning of the subarray until the current sum is no longer negative, then updates the end index if the current sum equals the maximum sum and the length of the subarray is greater than the previous one. Finally, it returns the length of the maximum subarray by subtracting the start index from the end index and adding 1.

function maxSubarraySize(arr) { let maxSum = -Infinity; let currentSum = 0; let start = 0; let end = 0; for (let i = 0; i < arr.length; i++) { currentSum += arr[i]; if (currentSum > maxSum) { maxSum = currentSum; end = i; } while (currentSum < 0) { currentSum -= arr[start]; start++; } if (currentSum === maxSum && i - start > end - start) { end = i; } } return end - start + 1; } // Example usage: const arr = [1, -2, 1, 1, -2, 1]; console.log("Array:", JSON.stringify(arr)); const size = maxSubarraySize(arr); console.log("Size of the Subarray with Maximum Sum:", size);

Let's see the output with some examples for better understanding.

### Example 1

**Input** − Given an array of integers, a[]= {1, -2, 1, 1, -2, 1}

**Output **− 2

**Explanation** − The subarray with consecutive elements and the largest sum is {1, 1}. Hence, the length is 2.

### Example 2

**Input** − Given array of all negative integers, a[]= {-2, -5, -8, -3, -1, -7}

**Output **-1

**Explanation** − In this case, the maximum subarray would be [-1] with a sum of -1. Therefore, the length of the subarray is -1.

## Conclusion

The size of the subarray with the maximum sum is a common problem when working with arrays in programming. The algorithm for solving this problem involves iterating through the array and keeping track of the current sum and the maximum sum seen so far. By implementing this algorithm in JavaScript, we can write a program that efficiently finds the size of the subarray with the maximum sum for any given array of integers.