[O(n)] Simple and clean, sliding window
Loop through and consider number of subarrays that end at j.\nSimple sliding window of the current alternating block.\nint j - end of alternating block\nint i -
Loop through and consider number of subarrays that end at `j`.\nSimple sliding window of the current alternating block.\n`int j` - end of alternating block\n`int i` - minimum first index of current alternating block\n`int res` - total count of alternating subarrays\n\nAll subarrays $[i,j] , [i+1,j], \\ldots[j,j]$ are valid alternating subarrays, this yields `j-i+1` added to our result.\n\n# Code\n```\nclass Solution {\npublic:\n    long long countAlternatingSubarrays(vector<int>& nums) {\n        long long res = 1;\n        int i =0; // index to start alternating block\n        for(int j = 1; j< nums.size(); j++){\n            if(nums[j-1] == nums[j]){\n                i = j; //new alternating block\n            }\n            res += j-i+1;\n        }\n        return res;\n    }\n};\n```
['C++']