Beats 98%
\n# Code\n\nclass Solution {\n    public long countAlternatingSubarrays(int[] nums) {\n        int prev = nums[0];\n        long count = 1;\n        long ans = 
\n# Code\n```\nclass Solution {\n    public long countAlternatingSubarrays(int[] nums) {\n        int prev = nums[0];\n        long count = 1;\n        long ans = 0;\n        for(int i = 1;i<nums.length;i++){\n            if(nums[i]==1-prev){\n                count++;    \n            }else{\n                ans = ans+ (count*(count+1)/2);\n                count = 1;\n            }\n            prev = nums[i];  \n        }\n        return ans+(count*(count+1)/2);\n    }\n}\n```
['Java']