💡0101... & 1010....
Approach\nFormed two arrays of size n of 0,1,0,1\u2026(say arr1) and 1,0,1,0\u2026.(say arr2).\nNow iterating i(0 to n), find the maximum subarray common betwee
# Approach\nFormed two arrays of size n of 0,1,0,1\u2026(say arr1) and 1,0,1,0\u2026.(say arr2).\nNow iterating i(0 to n), find the maximum subarray common between given array nums and arr1 and set i to end of this subarray. Keep adding len*(len+1)/2 for every such subarray length.\nSimilarly do this with arr2 and nums.\n\n\n# Complexity\n- Time complexity:O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    long long countAlternatingSubarrays(vector<int>& nums) {\n        int n=nums.size();\n        \n        //forming 0,1,0,1....\n        vector<int> a(n,0);\n        int curr=0;\n        for(int i=0 ; i<n ; i++){\n            a[i]=curr;\n            curr^=1;\n        }\n        \n        //forming 1,0,1,0....\n        vector<int> b(n,0);\n        curr=1;\n        for(int i=0 ; i<n ; i++){\n            b[i]=curr;\n            curr^=1;\n        }\n        \n        //common subarrays for nums and a\n        long long ans=0;\n        for(long long i=0 ; i<n ; i++){\n            long long j=i;\n            while(i<n && nums[i]==a[i]){\n                i++;\n            }\n            long long l=(i-j);\n            ans+=(l*(l+1))/(long long)2;\n        }\n        \n        //common subarrays for nums and b\n        for(long long i=0 ; i<n ; i++){\n            long long j=i;\n            while(i<n && nums[i]==b[i]){\n                i++;\n            }\n            long long l=(i-j);\n            ans+=(l*(l+1))/(long long)2;\n        }\n        \n        return ans;\n    }\n};\n```
['C++']