✅✅Faster || Easy To Understand || C++ Code
Using Dp\n\n Time Complexity :- O(N)\n\n Space Complexity :- O(1)\n\n\nclass Solution {\npublic:\n    int getMaxLen(vector<int>& nums) {\n        \n        int 
* ***Using Dp***\n\n* ***Time Complexity :- O(N)***\n\n* ***Space Complexity :- O(1)***\n\n```\nclass Solution {\npublic:\n    int getMaxLen(vector<int>& nums) {\n        \n        int n = nums.size();\n        \n        // pos_len will keep track of maximum length of subarray with positive product\n        \n        int pos_len = 0;\n        \n        // neg_len will track keep of maximum length of subarray with negative product\n        \n        int neg_len = 0;\n        \n        int maxi = 0;\n        \n        for(int i = 0; i < n; i++)\n        {\n            if(nums[i] == 0)\n            {\n                // now positive product and negative product both will be 0\n                \n                // so, pos_len and neg_len will be 0\n                \n                pos_len = 0;\n               \n                neg_len = 0;\n            }\n            else if(nums[i] > 0)\n            {\n                pos_len++;\n                \n                // if neg_len is zero it means negative product is 0, so starting with curr element will not lead to negative product, that\'s why it will remain 0\n                \n                // otherwise increment neg_len, b/c multiplying to neg_product with positive element will increment the length of neg_len\n                \n                if(neg_len != 0)\n                {\n                    neg_len++;\n                }\n            }\n            else if(nums[i] < 0)\n            {\n                // here we will swap these (pos_len and neg_len)\n                \n                // store the neg_len\n                \n                int prev_neg_len = neg_len;\n                \n                // increment the neg_len\n                \n                neg_len = pos_len + 1;\n                \n                // if prev_neg_len is zero it means negative product is 0, so starting with curr element will not lead to positive product, that\'s why it will remain 0\n                \n                // otherwise update pos_len, b/c multiplying to neg_product with negative element will make it positive\n                \n                if(prev_neg_len == 0)\n                {\n                    pos_len = 0;\n                }\n                else\n                {\n                    pos_len = prev_neg_len + 1;\n                }\n            }\n            \n            // update maxi\n            \n            maxi = max(maxi, pos_len);\n        }\n        \n        return maxi;\n    }\n};\n```
['Dynamic Programming', 'Greedy', 'C', 'C++']