Very Easy Java solution 🔥 Beats 97 % of Java Users .
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nclass Solution {\n    public long countAlternatingSubarrays(int[] nums) {\n        long rs = 0 ; \n        int n = nums.length ; \n        long curr = 0 ; \n        for(int i=0 ; i<n ; ++i ){\n            curr++ ; \n            while( i+1<n && (nums[i] ^ nums[i+1] ) == 1  ){\n                curr++  ;\n                i++ ;\n                \n            }\n            rs += curr*(curr+1)/2 ;\n            curr = 0 ;\n        }\n        return rs ;\n    }\n}\n```
['Java']