Easy intuitive approach explained by striver
Intuition\n Describe your first thoughts on how to solve this problem. \nWhenever you encounter a negative number,it can either->\n1-contribute to prefix [2,3,-
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWhenever you encounter a negative number,it can either->\n1-contribute to prefix [2,3,-4,-2,1]\n2-contribute to suffix [2,3,4,-2,-1]\n3-nothing [2,3,4,-2,1]\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->o(n)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->o(1)\n\n# Code\n```\nclass Solution {\npublic:\n    int getMaxLen(vector<int>& nums) \n    {\n        //-2 3 4-1 -3 0 1 2 3 4\n        int n=nums.size();\n        int pos=0,neg=0,suff=0,ans=0;\n        for(int i=0;i<n;i++)\n        {\n            if(nums[i]==0){pos=0,neg=0;}\n            if(nums[i]>0)pos++;\n            if(nums[i]<0)neg++;\n\n            if(neg%2==0)ans=max(ans,pos+neg);\n        }\n\n        pos=0,neg=0;\n        for(int i=n-1;i>=0;i--)\n        {\n            if(nums[i]==0){pos=0,neg=0;}\n            if(nums[i]>0)pos++;\n            if(nums[i]<0)neg++;\n\n            if(neg%2==0)ans=max(ans,pos+neg);\n        }\n        return ans;\n\n    }\n};\n```
['C++']