Optimised Solution
\n# Complexity\n- Time complexity:\nO(N)\n\n- Space complexity:\nO(1)\n\n# Code\n\nclass Solution {\npublic:\n    long long countAlternatingSubarrays(vector<int
\n# Complexity\n- Time complexity:\nO(N)\n\n- Space complexity:\nO(1)\n\n# Code\n```\nclass Solution {\npublic:\n    long long countAlternatingSubarrays(vector<int>& nums) {\n        long long c=0,s=1,i=1; \n        while(i<nums.size())\n        {\n            if(nums[i]!=nums[i-1]) \n            {\n                s++;\n            } \n            else \n            {\n                c+=s*(s+1)/2;\n                s=1; \n            }\n            i++;\n        }\n        c+=s*(s+1)/2;\n        return\xA0c;\n    }\n};\n```
['C++']