Simple Maths || C++ Solution
\n\n# Code\n\nclass Solution {\npublic:\n    long long countAlternatingSubarrays(vector<int>& nums) {\n        long long ans=0;\n        long long len=1;\n     
\n\n# Code\n```\nclass Solution {\npublic:\n    long long countAlternatingSubarrays(vector<int>& nums) {\n        long long ans=0;\n        long long len=1;\n        for(int i=1;i<nums.size();i++)\n        {\n            if(nums[i]!=nums[i-1])\n            {\n                len++;\n            }\n            else\n            {\n                ans=ans+((len*(len+1))/2);\n                len=1;\n            }\n        }\n        ans=ans+((len*(len+1))/2);\n        return ans;\n    }\n};\n```
['Array', 'Math', 'C++']