✅ C++ Solution ✅ ||  Sliding window || Optimal
Approach\n Describe your approach to solving the problem. \n\nUses Sliding window\n\nDry run this example for better understanding : [1,0,1,0,0,1]\n\n# Complexi
# Approach\n<!-- Describe your approach to solving the problem. -->\n\n**Uses Sliding window**\n\n*Dry run this example for better understanding* : [1,0,1,0,0,1]\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        \n        int n = nums.size();        \n        long long ans = 0;\n        int i=0,j=1;\n        int t = 0;\n        \n        while(j<n){\n            \n            if( nums[t] != nums[j] ){\n                // number of alternate subarray ending at \'j\'\n                ans +=  j - i;\n                t++;\n            }\n            \n            else{\n                i = j;\n                t = j;\n            }\n\n            j++;\n            \n        }   \n        \n        // add \'n\' as, each element itself is a alternate subbarray\n        return ans + n;\n        \n    }\n};\n```
['Sliding Window', 'C++', 'Java']