Beats 100% | Detailed Explanation | From Beginner to Pro | Python
\nclass Solution:\n    def findMax(self,start,end,totalNegitive,firstNegitive,lastNegitive):\n        if totalNegitive%2==0:\n            return end+1-start\n  
```\nclass Solution:\n    def findMax(self,start,end,totalNegitive,firstNegitive,lastNegitive):\n        if totalNegitive%2==0:\n            return end+1-start\n        else:\n            if firstNegitive:\n                return max(lastNegitive-start,end-firstNegitive)\n            else:\n                return end-start\n\n    def getMaxLen(self, nums: List[int]) -> int:\n        start=0\n        totalNegitive=0\n        firstNegitive=None\n        lastNegitive=None\n        result=0\n        \n\n        for end,value in enumerate(nums):\n            if value<0:\n                totalNegitive+=1\n                if firstNegitive is None:\n                    firstNegitive=end\n                lastNegitive=end\n            elif value==0:\n                if start==end:\n                    start+=1\n                    continue\n\n                    \n                result=max(result,self.findMax(start,end-1,totalNegitive,firstNegitive,lastNegitive))\n                \n                start=end+1\n                lastNegitive=None\n                firstNegitive=None\n                totalNegitive=0\n        result=max(result,self.findMax(start,end,totalNegitive,firstNegitive,lastNegitive))\n        return result\n```\n\t\t\n\t\tAs per hints given by Leetcode.\n1. If all the elements in an array are positive, result would be size of an array.\n2. If total number of negitive values are even, result would be again size of an array.\n3. Tricky part is with total number of negitive values are odd. For instance,\narray=[ 0 , -1 , 2 , -3 , 4 , -5 , 6 ]. we have two choices, either - remove the prefix till the first negative element in this array, or remove the suffix starting from the last negative element in this array. There will be two choice, [-1 , 2, -3 , 4] (size: 4) [2 , -3 , 4 , -5 , 6] (size: 5). Hence Size 5 will be returned. \n\n4. As multiplying any number with 0 results to 0, we create subarrays where 0 is excluded. For instance, array=[1 , 2 , 3 , 0 , 4 , 5 , 6 ] --->Will be converted into two independent subarray ---> [1 , 2 , 3]  ,  [4 , 5 , 6 ]\n \nPS: If total count of negitve number is odd, we need to make it even for making it maximum positive product. So we remove one negitive element from the array either by - removing the prefix till the first negative element or remove the suffix starting from the last negative element in the array.\n\n```\ndef getMaxLen(self, nums: List[int]) -> int:\n        start=0 \n        totalNegitive=0\n        firstNegitive=None\n        lastNegitive=None\n        result=0\n```\nstart: Starting index of a subarray.\ntotalNegitive: Count of total negitive in a subarray\nfirstNegitive: index of first negitive element\nlastNegitive: index of last negitive element\nresult: maximum length of array/sub array\n\n```\nfor end,value in enumerate(nums):\n```\nTraversing each element with index, value. Index is end value of a subarray\n\n\n```\nif value<0:\n\t\ttotalNegitive+=1\n\t\tif firstNegitive is None:\n\t\t\tfirstNegitive=end\n\t\tlastNegitive=end\n```\nIf element in array is negitive, we increment totalNegitive for total count of negitive number in an array/subarray. \n\nIf "firstNegitive" variable is None, we add first negitive element index to "firstNegitive". lastNegitve will have last negitive element`s index.\n\n```\nelif value==0:\n\tif start==end:\n\t\tstart+=1\n\t\tcontinue\n```\n\nIf element is 0, we want to create sub-array, but we check , if subarray length is not 0, i.e start!=end\n\nI created another function, which returns maximum length from given subarray.\n \n```\n    def findMax(self,start,end,totalNegitive,firstNegitive,lastNegitive):\n        if totalNegitive%2==0:\n            return end+1-start\n        else:\n            if firstNegitive:\n                return max(lastNegitive-start,end-firstNegitive)\n            else:\n                return end-start\n```\n\nIf totalNegitive is even, we return size of array.\nelse:\nif there exist negitive element, we return maximum length from firstNegitive to end of array, or starting of array to lastNegitive\nelse:\nwe return length of whole array\n\n\n```\nstart=end+1\nlastNegitive=None\nfirstNegitive=None\ntotalNegitive=0\n```\nAs "0" element in an array creates subarray,\nwe resets above variables to compute subarray.\n\n\n**SUUGESTIONS OR EDITS ARE WELCOME. THIS CAN BE FURTHER OPTIMIZED. THANKS**\nYou can follow me at LinkedIn [https://www.linkedin.com/in/kshitijbhatnagar/](http://)\n![image](https://assets.leetcode.com/users/images/2652c921-5467-46aa-a862-56ebe58e055a_1632675894.4766622.png)\n
['Dynamic Programming', 'Python', 'Python3']