🔥Easy to Understand | Clean Code | C++ |
Code\n\nclass Solution {\npublic:\n    long long countAlternatingSubarrays(vector<int>& nums) {\n        long long left = 0;\n        long long res = nums.size(
# Code\n```\nclass Solution {\npublic:\n    long long countAlternatingSubarrays(vector<int>& nums) {\n        long long left = 0;\n        long long res = nums.size();\n        long long i;\n        for (i = 1; i < nums.size(); i++) \n        {\n            if (nums[i] != nums[i - 1])\n                continue;\n\n            long long len = i - left;\n            res += (len * (len - 1) / 2);\n            left = i;\n        }\n        \n        long long len = i - left;\n        if (i >= 2 && left != i)\n            res += (len * (len - 1) / 2);\n\n        return res;\n    }\n};\n```
['C++']