C++ | Sliding Window | Counting | Explained
Approach\nWe know our subarray that contribute to answer must now have any adjacent values same. \n\tSo use a variable prev to store the last index whem element
**Approach**\nWe know our subarray that contribute to answer must now have any adjacent values same. \n\tSo use a variable `prev` to store the last index whem element is different. Now iterate over the array if adjacent are not same,  update `answer += (i - prev)`. If same set `prev = current` Index and iterate ahead.  \n\n```\nclass Solution {\npublic:\n    long long countAlternatingSubarrays(vector<int>& nums) {\n        long long ans = 0; \n        \n        //count all same \n        long long n = nums.size(); \n        ans = n;  \n        long long cnt = 1; \n        long long prev = 0; \n        for(long long i = 1; i < n; i++) {\n            if(nums[i] != nums[i - 1]) {\n                ans += (i - prev); \n                \n            } else {\n               prev = i; \n            }\n        }\n        return ans; \n    }\n};\n```
['Counting', 'C++']