Given an integer array nums, find a contiguous non-empty subarray within the array that has the largest product, and return the product.

The test cases are generated so that the answer will fit in a 32-bit integer.

A subarray is a contiguous subsequence of the array.

&nbsp;
Example 1:

Input: nums = [2,3,-2,4]
Output: 6
Explanation: [2,3] has the largest product 6.


Example 2:

Input: nums = [-2,0,-1]
Output: 0
Explanation: The result cannot be 2, because [-2,-1] is not a subarray.


&nbsp;
Constraints:


	1 &lt;= nums.length &lt;= 2 * 104
	-10 &lt;= nums[i] &lt;= 10
	The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.


class Solution:
    def maxProduct(self, nums: List[int]) -> int:
        prod=1
        maxprod=-100000000
        for i in range(len(nums)): # traverse from L-R so that we get max 
            prod*=nums[i]
            maxprod=max(maxprod,prod)
            if prod==0:
                prod=1

        prod=1
        for i in range(len(nums)-1,-1,-1): #if 0 or -ve present at starting then find from back
            prod*=nums[i]
            maxprod=max(maxprod,prod)
            if prod==0:
                prod=1

        return maxprod
class Solution {
    public int maxProduct(int[] nums) {
        int ans = Integer.MIN_VALUE;
        int m = 1;
        for(int i=0; i< nums.length; i++){
            m*=nums[i];
            ans = Math.max(m, ans);
            if(m == 0) m=1;
        }
        int n = 1;
        for(int i=nums.length-1; i>=0; i--){
            n*=nums[i];
            ans = Math.max(n, ans);
            if(n == 0) n=1;
        }
        return ans;
    }
}
class Solution {
public:
    int maxProduct(vector<int>& nums) {
        int n = nums.size();
        int negPro = 1;
        int posPro = 1;
        int CHECK_ZERO = 0;
        int res = INT_MIN;
        for(int i = 0; i < n; i++)
        {
            if(nums[i] == 0)
            {
                posPro = 1;
                negPro = 1;
                CHECK_ZERO = 1;
            }
            int numPos = posPro * nums[i];
            int numNeg = negPro * nums[i];
            posPro = max(numPos, max(numNeg, nums[i]));
            negPro = min(numPos, min(numNeg, nums[i]));
            res = max(posPro,res);
        }
        return (CHECK_ZERO ? max(0,res) : res);
    }
};
/**
 * @param {number[]} nums
 * @return {number}
 */
var maxProduct = function(nums) {
    const n = nums.length - 1;
    let ans = nums[0];
    let l = 1, r = 1;

    for (let i = 0; i < nums.length; i++) {
        l = (l ? l : 1) * nums[i];
        r = (r ? r : 1) * nums[n - i];
        ans = Math.max(ans, Math.max(l, r));
    }

    return ans;
};
Maximum Product Subarray