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, -3, 0, -2, -40}
Output: 80 // The subarray is {-2, -40}
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.
#include <bits/stdc++.h>
using namespace std;
int maxSubarrayProduct( int arr[], int n)
{
int max_ending_here = 1;
int min_ending_here = 1;
int max_so_far = 1;
int flag = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] > 0) {
max_ending_here = max_ending_here * arr[i];
min_ending_here = min(min_ending_here * arr[i], 1);
flag = 1;
}
else if (arr[i] == 0) {
max_ending_here = 1;
min_ending_here = 1;
}
else {
int temp = max_ending_here;
max_ending_here = max(min_ending_here * arr[i], 1);
min_ending_here = temp * arr[i];
}
if (max_so_far < max_ending_here)
max_so_far = max_ending_here;
}
if (flag == 0 && max_so_far == 1)
return 0;
return max_so_far;
}
int main()
{
int arr[] = { 1, -2, -3, 0, 7, -8, -2 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << "Maximum Sub array product is "
<< maxSubarrayProduct(arr, n);
return 0;
}
|
Output:Maximum Sub array product is 112
Time Complexity: O(n)
Auxiliary Space: O(1)
import java.io.*;
class ProductSubarray {
static int min( int x, int y) { return x < y ? x : y; }
static int max( int x, int y) { return x > y ? x : y; }
static int maxSubarrayProduct( int arr[])
{
int n = arr.length;
int max_ending_here = 1 ;
int min_ending_here = 1 ;
int max_so_far = 1 ;
int flag = 0 ;
for ( int i = 0 ; i < n; i++) {
if (arr[i] > 0 ) {
max_ending_here = max_ending_here * arr[i];
min_ending_here = min(min_ending_here * arr[i], 1 );
flag = 1 ;
}
else if (arr[i] == 0 ) {
max_ending_here = 1 ;
min_ending_here = 1 ;
}
else {
int temp = max_ending_here;
max_ending_here = max(min_ending_here * arr[i], 1 );
min_ending_here = temp * arr[i];
}
if (max_so_far < max_ending_here)
max_so_far = max_ending_here;
}
if (flag == 0 && max_so_far == 1 )
return 0 ;
return max_so_far;
}
public static void main(String[] args)
{
int arr[] = { 1 , - 2 , - 3 , 0 , 7 , - 8 , - 2 };
System.out.println( "Maximum Sub array product is "
+ maxSubarrayProduct(arr));
}
}
|
Output:Maximum Sub array product is 112
Time Complexity: O(n)
Auxiliary Space: O(1)
def maxsubarrayproduct(arr):
n = len (arr)
max_ending_here = 1
min_ending_here = 1
max_so_far = 1
flag = 0
for i in range ( 0 , n):
if arr[i] > 0 :
max_ending_here = max_ending_here * arr[i]
min_ending_here = min (min_ending_here * arr[i], 1 )
flag = 1
elif arr[i] = = 0 :
max_ending_here = 1
min_ending_here = 1
else :
temp = max_ending_here
max_ending_here = max (min_ending_here * arr[i], 1 )
min_ending_here = temp * arr[i]
if (max_so_far < max_ending_here):
max_so_far = max_ending_here
if flag = = 0 and max_so_far = = 1 :
return 0
return max_so_far
arr = [ 1 , - 2 , - 3 , 0 , 7 , - 8 , - 2 ]
print "Maximum product subarray is" , maxsubarrayproduct(arr)
|
Output:Maximum Sub array product is 112
Time Complexity: O(n)
Auxiliary Space: O(1)
Comments
Post a Comment