Javascript Program for Maximum Product Subarray
Last Updated :
18 Sep, 2024
Given an array that contains both positive and negative integers, find the product of the maximum product subarray. Expected Time complexity is O(n) and only O(1) extra space can be used.
Examples:
Input: arr[] = {6, -3, -10, 0, 2}
Output: 180 // The subarray is {6, -3, -10}
Input: arr[] = {-1, -3, -10, 0, 60}
Output: 60 // The subarray is {60}
Input: arr[] = {-2, -40, 0, -2, -3}
Output: 80 // The subarray is {-2, -40}
Naive Solution:
The idea is to traverse over every contiguous subarrays, find the product of each of these subarrays and return the maximum product from these results.
Below is the implementation of the above approach.
JavaScript
// Javascript program to find Maximum Product Subarray
/* Returns the product of max product subarray.*/
function maxSubarrayProduct(arr, n) {
// Initializing result
let result = arr[0];
for (let i = 0; i < n; i++) {
let mul = arr[i];
// traversing in current subarray
for (let j = i + 1; j < n; j++) {
// updating result every time
// to keep an eye over the maximum product
result = Math.max(result, mul);
mul *= arr[j];
}
// updating the result for (n-1)th index.
result = Math.max(result, mul);
}
return result;
}
// Driver code
let arr = [1, -2, -3, 0, 7, -8, -2];
let n = arr.length;
console.log("Maximum Sub array product is " + maxSubarrayProduct(arr, n));
OutputMaximum Sub array product is 112
Complexity Analysis:
- Time Complexity: O(N2)
- Auxiliary Space: O(1)
Efficient Solution:
The following solution assumes that the given input array always has a positive output. The solution works for all cases mentioned above. It doesn't work for arrays like {0, 0, -20, 0}, {0, 0, 0}.. etc. The solution can be easily modified to handle this case.
It is similar to Largest Sum Contiguous Subarray problem. The only thing to note here is, maximum product can also be obtained by minimum (negative) product ending with the previous element multiplied by this element. For example, in array {12, 2, -3, -5, -6, -2}, when we are at element -2, the maximum product is multiplication of, minimum product ending with -6 and -2.
JavaScript
// JavaScript program to find
// Maximum Product Subarray
/* Returns the product
of max product subarray.
Assumes that the given
array always has a subarray
with product more than 1 */
function maxSubarrayProduct(arr, n) {
// max positive product
// ending at the current position
let max_ending_here = 1;
// min negative product ending
// at the current position
let min_ending_here = 1;
// Initialize overall max product
let max_so_far = 0;
let flag = 0;
/* Traverse through the array.
Following values are
maintained after the i'th iteration:
max_ending_here is always 1 or
some positive product ending with arr[i]
min_ending_here is always 1 or
some negative product ending with arr[i] */
for (let i = 0; i < n; i++) {
/* If this element is positive, update
max_ending_here. Update min_ending_here only if
min_ending_here is negative */
if (arr[i] > 0) {
max_ending_here = max_ending_here * arr[i];
min_ending_here
= Math.min(min_ending_here * arr[i], 1);
flag = 1;
}
/* If this element is 0, then the maximum product
cannot end here, make both max_ending_here and
min_ending_here 0
Assumption: Output is alway greater than or equal
to 1. */
else if (arr[i] == 0) {
max_ending_here = 1;
min_ending_here = 1;
}
/* If element is negative. This is tricky
max_ending_here can either be 1 or positive.
min_ending_here can either be 1 or negative.
next max_ending_here will always be prev.
min_ending_here * arr[i] ,next min_ending_here
will be 1 if prev max_ending_here is 1, otherwise
next min_ending_here will be prev max_ending_here *
arr[i] */
else {
let temp = max_ending_here;
max_ending_here
= Math.max(min_ending_here * arr[i], 1);
min_ending_here = temp * arr[i];
}
// update max_so_far, if needed
if (max_so_far < max_ending_here)
max_so_far = max_ending_here;
}
if (flag == 0 && max_so_far == 0)
return 0;
return max_so_far;
}
// Driver program
let arr = [1, -2, -3, 0, 7, -8, -2];
let n = arr.length;
console.log("Maximum Sub array product is " + maxSubarrayProduct(arr, n));
OutputMaximum Sub array product is 112
Complexity Analysis:
- Time Complexity: O(n)
- Auxiliary Space: O(1)
Please refer complete article on Maximum Product Subarray for more details!
Similar Reads
Javascript Program for Largest Sum Contiguous Subarray Write an efficient program to find the sum of contiguous subarray within a one-dimensional array of numbers that has the largest sum. Kadane's Algorithm:Initialize: max_so_far = INT_MIN max_ending_here = 0Loop for each element of the array (a) max_ending_here = max_ending_here + a[i] (b) if(max_so_f
5 min read
Maximum Product Subarray Given an integer array, the task is to find the maximum product of any subarray. Examples:Input: arr[] = {-2, 6, -3, -10, 0, 2}Output: 180Explanation: The subarray with maximum product is {6, -3, -10} with product = 6 * (-3) * (-10) = 180Input: arr[] = {-1, -3, -10, 0, 60}Output: 60Explanation: The
15+ min read
Maximum Product Subarray | Added negative product case Given an array that contains both positive and negative integers, find the product of the maximum product subarray. Expected Time complexity is O(n) and only O(1) extra space can be used. The maximum product can be positive, negative or zero.Examples: Input : arr[] = {-2, -3, 0, -2, -40}Output : 80S
13 min read
Length of maximum product subarray Given an integer array arr[] of size N, the task is to find the maximum length subarray whose products of element is non zero. . Examples: Input: arr[] = [1, 1, 0, 2, 1, 0, 1, 6, 1] Output: 3 Explanation Possible subarray whose product are non zero are [1, 1], [2, 1] and [1, 6, 1] So maximum possibl
8 min read
Maximum subarray product modulo M Given an array, arr[] of size N and a positive integer M, the task is to find the maximum subarray product modulo M and the minimum length of the maximum product subarray. Examples: Input: arr[] = {2, 3, 4, 2}, N = 4, M = 5Output: Maximum subarray product is 4 Minimum length of the maximum product s
8 min read
Maximum product subset of an array Given an array a, we have to find the maximum product possible with the subset of elements present in the array. The maximum product can be a single element also.Examples: Input: a[] = { -1, -1, -2, 4, 3 }Output: 24Explanation : Maximum product will be ( -2 * -1 * 4 * 3 ) = 24 Input: a[] = { -1, 0 }
10 min read
Maximize product of a strictly increasing or decreasing subarray Given an array arr[] of size N, the task is to find the maximum product from any subarray consisting of elements in strictly increasing or decreasing order. Examples: Input: arr[] = { 1, 2, 10, 8, 1, 100, 101 } Output: 10100 Explanation: Increasing subarray with maximum product is {1, 100, 101}. The
14 min read
Find the Maximum Product of Two Integers in the Array using JavaScript ? Given an array of integers, we need to find the maximum product of two integers in the array using JavaScript.Example:Input: arr[] = {4, 6, 8, -5, -4, 4} Output: 48, {6,8} Explanation: Maximum product is 48, which is obtained by multiplying the numbers 6 and 8 of the array.Here are some common appro
8 min read
Maximum product of sum of two contiguous subarrays of an array Given an array arr[] of N positive integers, the task is to split the array into two contiguous subarrays such that the product of the sum of two contiguous subarrays is maximum. Examples: Input: arr[] = {4, 10, 1, 7, 2, 9} Output: 270 All possible partitions and their product of sum are: {4} and {1
10 min read
Maximize product of subarray sum with its maximum element Given an array arr[] consisting of N positive integers, the task is to find the maximum product of the subarray sum with the maximum element of that subarray. Examples: Input: arr[] = {2, -3, 8, -2, 5}Output: 88Explanation:The required maximum product can be obtained using subarray {8, -2, 5}Therefo
8 min read