C++ | Easy and Short Sliding Window Approach
Code\n\nclass Solution {\npublic:\n    long long countAlternatingSubarrays(vector<int>& nums) {\n        long long ans=0,i=0,j=0,n=nums.size();\n        vector<
# Code\n```\nclass Solution {\npublic:\n    long long countAlternatingSubarrays(vector<int>& nums) {\n        long long ans=0,i=0,j=0,n=nums.size();\n        vector<int> v;\n        while(j<n){\n            if(j==0 || nums[j]!=nums[j-1]){\n                ans+=j-i+1; //counting all subarrays till the index\n            }\n            else{\n                v.push_back(j);\n                i=j;\n            }\n            j++;\n        }\n        //including the single value subarrays if not already included in ans due to surrounding same digits\n        return ans+v.size();\n    }\n};\n\n```
['Two Pointers', 'Sliding Window', 'C++']