Suppose we have an array called nums, we have to find the product of elements of a contiguous subarray within an array (containing at least one number) which has the largest product. So if the array is [1,9,2,0,2,5], the output will be 18, as contiguous subarray [1,9,2] has max product.
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
class Solution(object): def maxProduct(self, nums): max_list =  * len(nums) min_list =  * len(nums) max_list = nums min_list = nums for i in range(1,len(nums)): max_list[i] = max(max(max_list[i-1]*nums[i],min_list[i-1]*nums[i]),nums[i]) min_list[i] = min(min(min_list[i-1]*nums[i],nums[i]),max_list[i-1]*nums[i]) return max(max_list) ob1 = Solution() print(ob1.maxProduct([1,9,2,0,2,5]))