Easiest Java Solution || Step by step
\n\n# Code\n\nclass Solution {\n    public long countAlternatingSubarrays(int[] nums) {\n        long count=1;\n        int ref=0; // changed when we encounter 
\n\n# Code\n```\nclass Solution {\n    public long countAlternatingSubarrays(int[] nums) {\n        long count=1;\n        int ref=0; // changed when we encounter that digit at previous index is same as digit at current index\n        int prev=nums[0];\n        for(int i=1;i<nums.length;i++){\n            if(prev!=nums[i])\n                count+=(i-ref); // (i-ref) all possible subarrays formed between index ref and index i, since between i and ref, no same digit is side by side\n            else\n                ref=i; // as no more subarrays including this index can be generated, so fresh start from this index\n\n            count+=1; // +1 (is for a subarray containing only the element at this particular index)\n            prev=nums[i];\n        }\n        return count;\n    }\n}\n```
['Java']