# JavaScript Program for Maximum Product Subarray

A subarray is part of an array formed by removing some or no prefixes of the array and removing some or no suffix elements of the given array. We will try to find the subarray that will contain the elements, and by multiplying all of them, we can get the maximum value. We will implement the code with the explanation. In this article, we will implement a JavaScript program for the Maximum product subarray.

### Introduction to Problem

In this problem, we are given an array and we have to pick any subarray from the given array, but the issue is the size of the subarray also doesn’t matter the thing is the product of all the elements of the subarray must be the maximum among all the subarrays.

If we are given the array which contains only positive numbers, then the result will always be the same given array and if any zero is present then the current array will be divided into many subarrays partitions at the zero and among all of them the subarray presents on the edges or in between the zeros.

Now, third case, if the array also contains the negative number then the thing is if there are two negative numbers or even negative numbers then we will get the positive result otherwise in the case of the odd number of negative numbers we will get the negative results.

For example −

Case1 − All positive numbers

Given array: 1 2 3 4 2 2 1


Case2 − All non-negative number

Given array: 1 2 3 0 4 5 1 0 9 2 0 0
Final answer: 20 by taking 4 5 1 as a subarray


Here we got three partitions: 1 2 3, 4 5 1, and 9 2, among all of these middle one is the maximum one.

Case3 − All integers

Given array: -40 -2 0 1 5 -9 -8 -6 0 9 8 3

Final answer: 360 by taking the 1 5 -9 -8, there are other subarrays with some high values but are less as compared to this


## Approach

There are two methods by which we can run this program which is: first is the naive approach in which first we will find all the subarrays of the given array and then we will multiply all of their numbers to find the largest multiplication. Let’s see its code −

### Example

// function to find the multiplication of the subarray
// with the largest result
function maxMulti(arr){
var len = arr.length
var ans = 0 // marking zero as the initial answer
var cur = 1 // marking variable to get the current subarray multiplication

// traversing over the array to get each subarray result
for(var i = 0;i<len; i++) {
cur = 1;
for(var j = i; j<len ;j++){
cur *= arr[j];
if(ans < cur){
ans = cur
}
}
}
console.log("The largest multiplication of subarray is: " + ans)
}

// definging the array's
arr1 = [1, 2, 3, 4, 2, 2, 1 ]
maxMulti(arr1)

// defining the second array
arr2 = [1, 2, 3, 0, 4, 5, 1, 0, 9, 2, 0, 0]
maxMulti(arr2)

// defining the third array
arr3 = [-40, -2, 0, 1, 5, -9, -8, -6, 0, 9, 8, 3]
maxMulti(arr3)

### Time and Space Complexity

The time complexity of the above code is O(N*N), where N is the length of the array and the space complexity of the above code is O(1).

The time complexity of the above code is not good as it could be better and for that, we are going to write another approach in which we are going to make time complexity better.

### Example

In this code, we will use the concept of the partition and the number theory, let’s see the code first

// function to find the multiplictaion of subarray
// with the largest result

function maxMulti(arr){
var max_ending = 1;
var min_ending = 1;
var ans = 0;
var temp= 0;
var n = arr.length
for(var i = 0; i < n; i++) {
if (arr[i] > 0) {
max_ending = max_ending * arr[i];
if(1 < min_ending * arr[i]) {
min_ending = 1
}
else
min_ending = min_ending * arr[i]
temp = 1;
}
else if (arr[i] == 0) {
max_ending = 1;
min_ending = 1;
} else {
var flag = max_ending;
max_ending = min_ending * arr[i] > 1 ? min_ending * arr[i] : 1;
min_ending = flag * arr[i];
}

// update max_so_far, if needed
if (ans < max_ending)
ans = max_ending;
}
if (temp == 0 && ans == 0){
console.log("The largest multiplication of subarray is: " + 0)
} else
console.log("The largest multiplication of subarray is: " + ans)
}

// defining the array's
arr1 = [1, 2, 3, 4, 2, 2, 1 ]
maxMulti(arr1)

// defining the second array
arr2 = [1, 2, 3, 0, 4, 5, 1, 0, 9, 2, 0, 0]
maxMulti(arr2)

// defining the third array
arr3 = [-40, -2, 0, 1, 5, -9, -8, -6, 0, 9, 8, 3]
maxMulti(arr3)

### Time and Space Complexity

The space complexity of the above code is O(1) as we are not using any extra space in the code. We are traversing over the array once which means there is a total N number of iterations, so the time complexity of the above code is O(N).

## Conclusion

In this article, we have implemented a JavaScript program for the Maximum product subarray. A subarray is part of an array formed by removing some or no prefixes of the array and removing some or no suffix elements of the given array. We have implemented two approaches with O(1) space complexity and O(N*N) and O(N) time complexity.

Updated on: 30-Mar-2023

160 Views

##### Kickstart Your Career

Get certified by completing the course