3101. Count Alternating Subarrays
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    long long countAlternatingSubarrays(vector<int>& nums) {\n        long long cnt = 0;\n        int i = 0;\n        int n = nums.size();\n        int j = 0;\n        while(i < n){\n            j = i;\n            while(j<n-1 && nums[j] != nums[j+1]) j++;\n            long long num = j+1-i;\n            num = (long long) num * (num+1)/2;\n            cnt += num;\n            i = j+1;\n        }\n        return cnt;\n    }\n};\n```
['C++']