Simple java solution
Complexity\n- Time complexity: O(n)\n\n- Space complexity: O(1)\n\n# Code\n\nclass Solution {\n    public long countAlternatingSubarrays(int[] nums) {\n        
# Complexity\n- Time complexity: $$O(n)$$\n\n- Space complexity: $$O(1)$$\n\n# Code\n```\nclass Solution {\n    public long countAlternatingSubarrays(int[] nums) {\n        long maxi=0;\n        int i=0,j=0;\n        for(i=0,j=0;i<nums.length-1;i++)\n        {\n            if(nums[i]==nums[i+1])\n            {\n                int val = i-j+1;\n                maxi+=(long)val*((long)(val+1))/2;\n                j=i+1;\n            }\n        }\n        int val = i-j+1;\n        maxi+=(long)val*((long)(val+1))/2;\n        return maxi;\n    }\n}\n```
['Java']