# 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}
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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++

 `// C++ program to find maximum product subarray ` `#include ` `using` `namespace` `std; ` ` `  `// Function to find maximum product subarray ` `int` `maxProduct(``int``* arr, ``int` `n) ` `{ ` `    ``// Variables to store maximum and minimum ` `    ``// product till ith index. ` `    ``int` `minVal = arr; ` `    ``int` `maxVal = arr; ` ` `  `    ``int` `maxProduct = arr; ` ` `  `    ``for` `(``int` `i = 1; i < n; i++) { ` ` `  `        ``// When multiplied by -ve number, ` `        ``// maxVal becomes minVal ` `        ``// and minVal becomes maxVal. ` `        ``if` `(arr[i] < 0) ` `            ``swap(maxVal, minVal); ` ` `  `        ``// maxVal and minVal stores the ` `        ``// product of subarray ending at arr[i]. ` `        ``maxVal = max(arr[i], maxVal * arr[i]); ` `        ``minVal = min(arr[i], minVal * arr[i]); ` ` `  `        ``// Max Product of array. ` `        ``maxProduct = max(maxProduct, maxVal); ` `    ``} ` ` `  `    ``// Return maximum product found in array. ` `    ``return` `maxProduct; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `arr[] = { -1, -3, -10, 0, 60 }; ` ` `  `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` ` `  `    ``cout << ``"Maximum Subarray product is "` `         ``<< maxProduct(arr, n) << endl; ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to find maximum product subarray ` `import` `java.io.*; ` ` `  `class` `GFG { ` ` `  `    ``// Function to find maximum product subarray ` `    ``static` `int` `maxProduct(``int` `arr[], ``int` `n) ` `    ``{ ` `         `  `        ``// Variables to store maximum and minimum ` `        ``// product till ith index. ` `        ``int` `minVal = arr[``0``]; ` `        ``int` `maxVal = arr[``0``]; ` `     `  `        ``int` `maxProduct = arr[``0``]; ` `     `  `        ``for` `(``int` `i = ``1``; i < n; i++) ` `        ``{ ` `     `  `            ``// When multiplied by -ve number, ` `            ``// maxVal becomes minVal ` `            ``// and minVal becomes maxVal. ` `            ``if` `(arr[i] < ``0``) ` `            ``{ ` `                ``int` `temp = maxVal; ` `                ``maxVal = minVal; ` `                ``minVal =temp; ` `             `  `            ``} ` `     `  `            ``// maxVal and minVal stores the ` `            ``// product of subarray ending at arr[i]. ` `            ``maxVal = Math.max(arr[i], maxVal * arr[i]); ` `            ``minVal = Math.min(arr[i], minVal * arr[i]); ` `     `  `            ``// Max Product of array. ` `            ``maxProduct = Math.max(maxProduct, maxVal); ` `        ``} ` `     `  `        ``// Return maximum product found in array. ` `        ``return` `maxProduct; ` `    ``} ` `     `  `    ``// Driver Code ` `    ``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)); ` `    ``} ` `} ` ` `  `// This code is contributed by anuj_67. `

## Python3

 `# Python 3 program to find maximum  ` `# product subarray ` ` `  `# Function to find maximum  ` `# product subarray ` `def` `maxProduct(arr, n): ` `     `  `    ``# Variables to store maximum and  ` `    ``# minimum product till ith index. ` `    ``minVal ``=` `arr[``0``] ` `    ``maxVal ``=` `arr[``0``] ` ` `  `    ``maxProduct ``=` `arr[``0``] ` ` `  `    ``for` `i ``in` `range``(``1``, n, ``1``): ` `         `  `        ``# When multiplied by -ve number, ` `        ``# maxVal becomes minVal ` `        ``# and minVal becomes maxVal. ` `        ``if` `(arr[i] < ``0``): ` `            ``temp ``=` `maxVal ` `            ``maxVal ``=` `minVal ` `            ``minVal ``=` `temp ` `             `  `        ``# maxVal and minVal stores the ` `        ``# product of subarray ending at arr[i]. ` `        ``maxVal ``=` `max``(arr[i], maxVal ``*` `arr[i]) ` `        ``minVal ``=` `min``(arr[i], minVal ``*` `arr[i]) ` ` `  `        ``# Max Product of array. ` `        ``maxProduct ``=` `max``(maxProduct, maxVal) ` ` `  `    ``# Return maximum product  ` `    ``# found in array. ` `    ``return` `maxProduct ` ` `  `# Driver Code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``arr ``=` `[``-``1``, ``-``3``, ``-``10``, ``0``, ``60``] ` ` `  `    ``n ``=` `len``(arr) ` ` `  `    ``print``(``"Maximum Subarray product is"``, ` `                     ``maxProduct(arr, n)) ` ` `  `# This code is contributed by ` `# Surendra_Gangwar `

## C#

 `// C# program to find  ` `// maximum product subarray ` `using` `System; ` ` `  `class` `GFG  ` `{ ` ` `  `    ``// Function to find maximum ` `    ``// product subarray ` `    ``static` `int` `maxProduct(``int` `[]arr,  ` `                          ``int` `n) ` `    ``{ ` `         `  `        ``// Variables to store  ` `        ``// maximum and minimum  ` `        ``// product till ith index. ` `        ``int` `minVal = arr; ` `        ``int` `maxVal = arr; ` `     `  `        ``int` `maxProduct = arr; ` `     `  `        ``for` `(``int` `i = 1; i < n; i++) ` `        ``{ ` `     `  `            ``// When multiplied by -ve  ` `            ``// number, maxVal becomes  ` `            ``// minVal and minVal  ` `            ``// becomes maxVal. ` `            ``if` `(arr[i] < 0) ` `            ``{ ` `                ``int` `temp = maxVal; ` `                ``maxVal = minVal; ` `                ``minVal = temp; ` `             `  `            ``} ` `     `  `            ``// maxVal and minVal stores  ` `            ``// the product of subarray  ` `            ``// ending at arr[i]. ` `            ``maxVal = Math.Max(arr[i],  ` `                              ``maxVal * arr[i]); ` `            ``minVal = Math.Min(arr[i],  ` `                              ``minVal * arr[i]); ` `     `  `            ``// Max Product of array. ` `            ``maxProduct = Math.Max(maxProduct,  ` `                                  ``maxVal); ` `        ``} ` `     `  `        ``// Return maximum product ` `        ``// found in array. ` `        ``return` `maxProduct; ` `    ``} ` `     `  `    ``// Driver Code ` `    ``public` `static` `void` `Main () ` `    ``{ ` `        ``int` `[]arr = {-1, -3, -10, 0, 60}; ` `        ``int` `n = arr.Length; ` `     `  `        ``Console.WriteLine(``"Maximum Subarray "` `+ ` `                                ``"product is "` `+ ` `                           ``maxProduct(arr, n)); ` `    ``} ` `} ` ` `  `// This code is contributed by anuj_67. `

## PHP

 ` `

Output:

```Maximum Sub array product is 60
```

Time Complexity: O( n )
Auxiliary Space: O( 1 )

Related Articles:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up A Coding Enthusiast Rails Developer

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

16

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.