[C++] Easy 2 Pass Solution | O(n) time, O(1) Space | Explained | Commented
We will be using Two pass Solution in which we use  TWO POINTERS \'f\' and  \'i\' ,\n\tf -> previous element of the last subarray ( Hence, initially -1 )\n\ti -
We will be using Two pass Solution in which we use  **TWO POINTERS** \'f\' and  \'i\' ,\n\tf -> previous element of the last subarray ( Hence, initially -1 )\n\ti -> current element of the current subarray\n\t\n\nCode:\n```\nint getMaxLen(vector<int>& nums) {\n        int ans = 0, f=-1;      \n        long long curr=1;\n\t\t\t\t\t\t\t\t\t\t\t\t//FIRST PASS ( left to right)\n        for(int i=0;i<nums.size();i++){\n            curr *= nums[i];       \n        /*\n         Since we Don\'t care about the magnitude of the result, we normalise it to positive or negative in both 1st and second pass\n        */       \n            if(curr>0){\n                ans  = max(ans,i-f);\n                curr = 1;\n            }\n            else if(curr==0){\n                curr=1;  // Since a 0 is encountered, we go on to choose the next subarray, so curr =-1\n                f   =i;    //pointing to the start for next subarray\n            }\n            else        //negative value, so normalizing it to -1\n                curr = -1;\n        }\n\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\t //SECOND PASS ( right to left )\n\t\t\t\t\t\t\t\t\t \n        curr=1;\n        f= nums.size();                                \n        for(int i=nums.size()-1;i>=0;i--){\n            curr *= nums[i];\n            if(curr>0){\n                ans  = max(ans,f-i);\n                curr = 1;\n            }\n            else if(curr==0){\n                curr=1;\n                f=i;\n            }\n            else\n                curr = -1;\n        }\n        return ans;\n    }\n```\n\n\t\n**Now You may ask why Two pass?**\nAns: \n\tLets see some examples using just First Pass\n1. \t\tinput : [2,0]\n\t\t\toutputs 1, which is currect\n2. \t\tinput : [0,2]\n\t\t\toutputs 1, which is also correct,\n3. \t\tinput :[-2,0]\n\t\t\toutputs 0, which is also correct,\n\t\t\t\n\t\t\t\tBUT\n4. \t\t**input : [-2,1]**\n\t\t\toutputs 0, which is WRONG, \n\t\t\tthe correct ans is 1\n\t\t\tSince we were moving from left to right we didn\'t got any positive multiplication result, hence outputs 0,\n\t\t\tTHEREFORE WE MUST CHECK FROM RIGHT SIDE TO LEFT SIDE TO AVOID MISSING ANY SUBARRAYS\n\t\t\tthe second pass will hence give the corrected ans, i.e, 1\n\t\t\t\n\t\t\tFeedbacks are welcome.\n\t\t\tUpvote if it Helped :)\n\t\t\t
['Two Pointers', 'C', 'C++']