Maximum Product Subarray | Set 3
Given an array A[] that contains both positive and negative integers, find the maximum product subarray.
Examples :
Input: A[] = { 6, -3, -10, 0, 2 }
Output: 180 // The subarray is {6, -3, -10}
Input: A[] = {-1, -3, -10, 0, 60 }
Output: 60 // The subarray is {60}
Input: A[] = { -2, -3, 0, -2, -40 }
Output: 80 // The subarray is {-2, -40}
The idea is to traverse array from left to right keeping two variables minVal and maxVal which represents the minimum and maximum product value till the ith index of the array. Now, if the ith element of the array is negative that means now the values of minVal and maxVal will be swapped as value of maxVal will become minimum by multiplying it with a negative number. Now, compare the minVal and maxVal.
The value of minVal and maxVal depends on the current index element or the product of the current index element and the previous minVal and maxVal respectively.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxProduct( int * arr, int n)
{
int minVal = arr[0];
int maxVal = arr[0];
int maxProduct = arr[0];
for ( int i = 1; i < n; i++) {
if (arr[i] < 0)
swap(maxVal, minVal);
maxVal = max(arr[i], maxVal * arr[i]);
minVal = min(arr[i], minVal * arr[i]);
maxProduct = max(maxProduct, maxVal);
}
return maxProduct;
}
int main()
{
int arr[] = { -1, -3, -10, 0, 60 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << "Maximum Subarray product is "
<< maxProduct(arr, n) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int maxProduct( int arr[], int n)
{
int minVal = arr[ 0 ];
int maxVal = arr[ 0 ];
int maxProduct = arr[ 0 ];
for ( int i = 1 ; i < n; i++)
{
if (arr[i] < 0 )
{
int temp = maxVal;
maxVal = minVal;
minVal =temp;
}
maxVal = Math.max(arr[i], maxVal * arr[i]);
minVal = Math.min(arr[i], minVal * arr[i]);
maxProduct = Math.max(maxProduct, maxVal);
}
return maxProduct;
}
public static void main (String[] args)
{
int arr[] = { - 1 , - 3 , - 10 , 0 , 60 };
int n = arr.length;
System.out.println( "Maximum Subarray product is "
+ maxProduct(arr, n));
}
}
|
Python3
def maxProduct(arr, n):
minVal = arr[ 0 ]
maxVal = arr[ 0 ]
maxProduct = arr[ 0 ]
for i in range ( 1 , n, 1 ):
if (arr[i] < 0 ):
temp = maxVal
maxVal = minVal
minVal = temp
maxVal = max (arr[i], maxVal * arr[i])
minVal = min (arr[i], minVal * arr[i])
maxProduct = max (maxProduct, maxVal)
return maxProduct
if __name__ = = '__main__' :
arr = [ - 1 , - 3 , - 10 , 0 , 60 ]
n = len (arr)
print ( "Maximum Subarray product is" ,
maxProduct(arr, n))
|
C#
using System;
class GFG
{
static int maxProduct( int []arr,
int n)
{
int minVal = arr[0];
int maxVal = arr[0];
int maxProduct = arr[0];
for ( int i = 1; i < n; i++)
{
if (arr[i] < 0)
{
int temp = maxVal;
maxVal = minVal;
minVal = temp;
}
maxVal = Math.Max(arr[i],
maxVal * arr[i]);
minVal = Math.Min(arr[i],
minVal * arr[i]);
maxProduct = Math.Max(maxProduct,
maxVal);
}
return maxProduct;
}
public static void Main ()
{
int []arr = {-1, -3, -10, 0, 60};
int n = arr.Length;
Console.WriteLine( "Maximum Subarray " +
"product is " +
maxProduct(arr, n));
}
}
|
PHP
<?php
function maxProduct(& $arr , $n )
{
$minVal = $arr [0];
$maxVal = $arr [0];
$maxProduct = $arr [0];
for ( $i = 1; $i < $n ; $i ++)
{
if ( $arr [ $i ] < 0)
{
$temp = $maxVal ;
$maxVal = $minVal ;
$minVal = $temp ;
}
$maxVal = max( $arr [ $i ], $maxVal * $arr [ $i ]);
$minVal = min( $arr [ $i ], $minVal * $arr [ $i ]);
$maxProduct = max( $maxProduct , $maxVal );
}
return $maxProduct ;
}
$arr = array ( -1, -3, -10, 0, 60 );
$n = sizeof( $arr );
echo "Maximum Subarray product is " .
maxProduct( $arr , $n ) . "\n" ;
?>
|
Javascript
<script>
function maxProduct(arr,n)
{
let minVal = arr[0];
let maxVal = arr[0];
let maxProduct = arr[0];
for (let i = 1; i < n; i++)
{
if (arr[i] < 0)
{
let temp = maxVal;
maxVal = minVal;
minVal =temp;
}
maxVal = Math.max(arr[i], maxVal * arr[i]);
minVal = Math.min(arr[i], minVal * arr[i]);
maxProduct = Math.max(maxProduct, maxVal);
}
return maxProduct;
}
let arr=[ -1, -3, -10, 0, 60 ];
let n = arr.length;
document.write( "Maximum Subarray product is "
+ maxProduct(arr, n));
</script>
|
Output
Maximum Subarray product is 60
Complexity Analysis:
- Time Complexity: O( n )
- Auxiliary Space: O( 1 )
Related Articles:
Last Updated :
26 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...