Simple C++ || Beginner Friendly || Beats 100%
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n\n1. Initialize variables n to store the size of the input array nums, cn
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n\n1. Initialize variables `n` to store the size of the input array `nums`, `cnt` to store the count of alternating subarrays, and `len` to store the length of the current alternating subarray, initially set to 1.\n\n2. Iterate through the array `nums` starting from index 1.\n   \n   a. If the current element (`nums[i]`) is different from the previous element (`nums[i-1]`), increment the `len` variable to extend the current alternating subarray.\n   \n   b. If the current element is the same as the previous one, calculate the number of subarrays formed by the current alternating subarray (using the formula for sum of natural numbers), add it to `cnt`, and reset `len` to 1 to start a new subarray.\n\n3. After the loop, add the count of the last alternating subarray (if any) to `cnt`.\n\n4. Finally, return the value of `cnt`, which represents the total number of alternating subarrays.\n\n# Complexity\n- Time complexity: O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    long long countAlternatingSubarrays(vector<int>& nums) {\n        long long n = nums.size(), cnt=0 ,len=1;\n        for (long long i=1; i<n; ++i) {\n            if(nums[i] != nums[i-1]) {\n                len++;\n            }else{\n                long long x = (len*(len + 1))/2;\n                cnt+=x, len = 1;\n            }\n        }\n        cnt += (len*(len + 1))/2;\n        return cnt;\n    }\n};\n```
['C++']