Maximum Product Subarray | Practice | GeeksforGeeks

Given an array Arr[] that contains N integers (may be positive, negative or zero). Find the product of the maximum product subarray.
Example 1:
Input:
N = 5
Arr[] = {6, -3, -10, 0, 2}
Output: 180
Explanation: Subarray with maximum product
is [6, -3, -10] which gives product as 180.

Example 2:
Input:
N = 6
Arr[] = {2, 3, 4, 5, -1, 0}
Output: 120
Explanation: Subarray with maximum product
is [2, 3, 4, 5] which gives product as 120.

Your Task:
You don't need to read input or print anything. Your task is to complete the function maxProduct() which takes the array of integers arr and n as parameters and returns an integer denoting the answer.
Note: Use 64-bit integer data type to avoid overflow.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 500
-102 ≤ Arri ≤ 102
0
sm21mtech120084 days ago
logic : for every iteration save max, min multiplication ,save maximum element of present and previous multiplication in res ie res=max(res,ma)
 
long long maxProduct(vector<int> arr, int n)
 {
    long long res=arr[0],ma=1,mi=1;
    for(auto i:arr)
    {
        long long t=i*ma;
 //if you dont save ma in t then min value would be changed 
        ma=max(t,max(i*mi,(long long)i));
        mi=min(t,min(i*mi,(long long)i));
        res=max(res,ma);
    }
    return res;
}
+1
prajjwal9934 days ago
JAVA 0(n)
 
class Solution {
    // Function to find maximum product subarray
    long maxProduct(int[] arr, int n) {
        long currmax = 1;
        long msf = Integer.MIN_VALUE;
        for(int i = 0; i < n; i++){
            currmax*=arr[i];
            msf=Math.max(msf,currmax);
            if(currmax==0){
                currmax=1;
            }
        }
        currmax=1;
        for(int i=n-1;i>=0;i--){
            currmax*=arr[i];
            msf=Math.max(msf,currmax);
            if(currmax==0){
                currmax=1;
            }
        }
        return msf;
    }
}
0
rajat8124 days ago
java solution O(n^2)
 long maxProduct(int[] arr, int n) {
        // code here
        long pro=1,max=Long.MIN_VALUE;
    for(int i=0;i<n;i++)
    {
        pro=1;
        for(int j=i;j<n;j++)
        {
            pro*=arr[j];
            max=Math.max(pro,max);
        }
        
    }
    return max;
    }
0
imramnazir2864 days ago
class Solution{
public:

	// Function to find maximum product subarray
	long long maxProduct(vector<int> arr, int n) {
	    // code here
	     long long mx=1,mn=1,ans=arr[0];
    for(int i=0;i<n;i++)
    {
        if(arr[i]==0)
           { mx=mn=1;
               continue;
           }
           if(arr[i]<0)
      {    
          long long t= mx;
          mx=max((long long)arr[i],arr[i]*mn);
          mn=min((long long)arr[i],arr[i]*t);
      }
      else
      {
          mx=max((long long)arr[i],arr[i]*mx);
          mn=min((long long)arr[i],arr[i]*mn);
      }
      ans=max(ans,mx);
    }
    return ans;
	    
	}
};
0
syedmallakbasheer4 days ago
cpp soln
long long maxProduct(vector<int> a, int n) {   long long glomp = a[0];   long long curmax = a[0];   long long curmin = a[0];   for(long long i=1;i<n;i++)   {       long long tmp = curmax*a[i];       curmax = max((long long)a[i],max(a[i]*curmax,a[i]*curmin));       curmin = min((long long)a[i],min(tmp,a[i]*curmin));       glomp = max(glomp,curmax);   }   return glomp;}
0
nitishmishra9374 days ago
Java Code
 
       long maxProduct(int[] arr, int n) {
		if (n == 1) {
			return arr[0];
		}

		long leftProduct = arr[0];
		long rightProduct = arr[n - 1];
		long max = Math.max(leftProduct, rightProduct);
		for (int i = 1; i < n; ++i) {
			leftProduct = leftProduct == 0 ? arr[i] : leftProduct * arr[i];
			rightProduct = rightProduct == 0 ? arr[n - i - 1] : rightProduct * arr[n - i - 1];
			max = Math.max(max, Math.max(leftProduct, rightProduct));
		}

		return max;
	}
0
vedratan85 days ago
 long long maxProduct(vector<int> arr, int n) {     long long   ans=arr[0],maximum=arr[0], minimum=arr[0];   for(int i=1;i<n;i++)   {       if(arr[i]<0)       {           swap(maximum, minimum);       }          maximum = (arr[i]>arr[i]*maximum)?arr[i]:arr[i]*maximum;          minimum = (arr[i]<arr[i]*minimum)?arr[i]:arr[i]*minimum;       ans= (ans>maximum)?ans:maximum;   }       return ans;}
0
rathodumang3196 days ago
Java Solution:
class Solution {
    long maxProduct(int[] arr, int n) {
        long ans = arr[0];
        long min = ans, max = ans;
        for(int i=1; i<n; i++){
            if(arr[i] < 0){
                long temp = max;
                max = min;
                min = temp;
            }
            max = Math.max(arr[i], max*arr[i]);
            min = Math.min(arr[i], min*arr[i]);
            ans = Math.max(ans, max);
        }
        return ans;
    }
}
0
amanmehar88
This comment was deleted.
+2
nareshshiva93171 week ago
long long maxProduct(vector<int> arr, int n) {    long long max = INT_MIN;    long long mul = 1;    for(int i=0;i<n;i++){        mul = mul * arr[i];        if(arr[i] == 0){            mul = 1;        }        if(mul>max){            max = mul;        }    }    mul = 1;    for(int i=n-1;i>=0;i--){        mul = mul * arr[i];        if(arr[i] == 0){            mul = 1;        }        if(mul>max){            max = mul;        }    }    return max;}
 
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested 
                                    against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code.
                                    On submission, your code is tested against multiple test cases consisting of all
                                        possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as
                                        the final solution code.
You can view the solutions submitted by other users from the submission tab.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems does not guarantee the 
                                correctness of code. On submission, your code is tested against multiple test cases
                                    consisting of all possible corner cases and stress constraints.