Simple C++ Solution || Counting
Approach\n1. The method iterates through the array nums starting from index 1.\nAt each iteration, it checks if the current element is equal to the previous ele
# Approach\n1. The method iterates through the array nums starting from index 1.\nAt each iteration, it checks if the current element is equal to the previous element.\n2. If they are equal, it means the alternating pattern is broken, so it calculates the length of the subarray and adds the count of alternating subarrays up to that point to the answer.\n3. It updates the`bg`(beginning) index to the current index to start counting the next subarray.\n4. After the loop, it calculates the length of the last subarray (from the last`bg`index to the end of the array) and adds its count to the answer.\n5. Finally, it returns the total count of alternating subarrays.\n\n# Complexity\n- Time complexity: `O(n)`\n- Space complexity: `O(1)`\n\n# Code\n```\nclass Solution\n{\npublic:\n    long long countAlternatingSubarrays(vector<int> &nums)\n    {\n        int bg = 0;\n        long long ans = 0;\n        for (int i = 1; i < nums.size(); i++)\n        {\n            if (nums[i] == nums[i - 1])\n            {\n                long long len = i - bg;\n                ans += (long long)(len * (len + 1) * 1ll) / 2;\n                bg = i;\n            }\n        }\n        long long len = nums.size() - bg;\n        ans += (long long)(len * (len + 1) * 1ll) / 2;\n        return ans;\n    }\n};\n```\n\n#### Why is it even medium? \uD83D\uDE44\n
['Sliding Window', 'C++']