<p>Given an integer array <code>nums</code>, find a <span data-keyword="subarray-nonempty">subarray</span> that has the largest product, and return <em>the product</em>.</p>

<p>The test cases are generated so that the answer will fit in a <strong>32-bit</strong> integer.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [2,3,-2,4]
<strong>Output:</strong> 6
<strong>Explanation:</strong> [2,3] has the largest product 6.
</pre>

<p><strong class="example">Example 2:</strong></p>

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

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>4</sup></code></li>
	<li><code>-10 &lt;= nums[i] &lt;= 10</code></li>
	<li>The product of any subarray of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>

class Solution:
    def maxProduct(self, nums: List[int]) -> int:

        def maxProduct_nozeros(nums):

            n = len(nums)
            if(n==1):
                return nums[0]

            prefix_prod = [0 for i in range(n)]
            prefix_prod[0] = nums[0]
            count_n = 1 if nums[0] < 0 else 0
            ans = nums[0]
            for i in range(1, n):
                prefix_prod[i] = nums[i]*prefix_prod[i-1]
                if(nums[i] < 0):
                    count_n += 1
                ans = max(ans, nums[i])
            
            if(count_n%2 == 0):
                return prefix_prod[n-1]

            # print (prefix_prod)
            
            for i in range(n):
                if(nums[i] < 0):
                    if(i == 0):
                        ans = max(ans, int(prefix_prod[n-1]/prefix_prod[0]))
                    elif i == n-1:
                        ans = max(ans, prefix_prod[i-1])
                    else:
                        ans = max(ans, prefix_prod[i-1], int(prefix_prod[n-1]/prefix_prod[i]))
            return ans
        
        n = len(nums)
        ans = max(nums)
        i, j = 0, 0
        while i<n:
            if(nums[i] == 0):
                i += 1
                continue
            else:
                j = i+1
                while (j < n and nums[j] != 0):
                    j += 1
                ans = max(ans, maxProduct_nozeros(nums[i:j]))
                i = j+1
        return ans
        
17800