O(n) | CPP | DP | Easy
Approach \n1) if no negative ans = length of array\n2) if even number of negative ans = length of array\n3) if odd number of negative then ans have two possibil
#### Approach \n1) if no negative ans = length of array\n2) if even number of negative ans = length of array\n3) if odd number of negative then ans have two possibilities\n\t* ingore the first negative and consider the remaining array\n\t* ignore the last negative and consider the previous array\n4) if array contains zeroes the partition the array into subarrays with 0 as boundaries and perform the above mentioned steps\n5) return the maximum length obtained\n\n#### Diagram\n![image](https://assets.leetcode.com/users/images/63b107dc-49ff-4bc3-a0f7-67faf1de099d_1656401361.3134112.jpeg)\n\n\n#### Code\n```\n    int getMaxLen(vector<int>& nums) {\n        int n=nums.size();\n        int ans=0;\n        int start, end, countNeg, lastNeg, firstNeg;\n        for(int i=0;i<n;){\n            start=i;\n            //marking the start of window in case if 0s present in array\n            while(start<n and nums[start]==0) start+=1;\n            end=start;\n            countNeg=0, lastNeg=-1, firstNeg=-1;\n            //marking the end of window in case if 0s present in array\n            while(end<n and nums[end]!=0){\n                //making a note of index of first and last negative element\n                if(nums[end]<0){\n                    countNeg+=1;\n                    if(firstNeg==-1) firstNeg=end;\n                    lastNeg=end;\n                }\n                end+=1;\n            }\n            //applying the conditions\n            if(countNeg%2==0) ans=max(ans,end-start);\n            else{\n                if(firstNeg!=-1) ans=max(ans,end-firstNeg-1);\n                if(lastNeg!=-1) ans=max(ans,lastNeg-start);\n            }\n            i=end+1;\n        }\n        return ans;\n    }\n```\n\n#### Analysis\nTime --> O(n)\nSpace --> O(1)\n\n#### Upvote if helpful\n#### Cheers
['Two Pointers', 'Dynamic Programming', 'C++']