JAVA easiest O(N) solution - sliding window approach
Approach\nJust apply the sliding window approach, change the initial pointer to new when a repeated digit is found. Focus on the fact that it is a binary array.
# Approach\nJust apply the sliding window approach, change the initial pointer to new when a repeated digit is found. Focus on the fact that it is a binary array. \nUse mathematical formula to calculate number of possible subsets.\n# Complexity\n- Time complexity: $$O(n)$$ \n\n# Code\n```java []\nclass Solution {\n    public long countAlternatingSubarrays(int[] nums) {\n        long ans=1;\n        int j=0;\n        for(int i=1;i<nums.length;i++){\n            if(nums[i-1]==nums[i]){\n                j=i;\n            }\n            ans+=(long)(i-j+1);\n        }\n        return ans;\n    }\n}\n```
['Java']