Simply counting number of subarrays || 100% Beat || O(n) solution || C++ code
Keep counting number of subarrays ending at index i and keep adding to the answer. As soon as we get same adjacent number bring the counter back to 1.\n\nTime C
**Keep counting number of subarrays ending at index i and keep adding to the answer. As soon as we get same adjacent number bring the counter back to 1.**\n\n**Time Complexity : O(n)\nSpace Complexity : O(1)**\n\n# Code\n```\nclass Solution {\npublic:\n    long long countAlternatingSubarrays(vector<int>& nums) {\n        int n = nums.size(), cnt = 1;\n        long long ans = cnt;\n        for(int i = 1; i < n; i++)\n        {\n            if(nums[i] != nums[i - 1])\n                cnt++;\n            else\n                cnt = 1;\n            \n            ans += cnt;\n        }\n        return ans;\n    }\n};\n```
['Array', 'Counting', 'C++']