Easy Java Solution || Beats 100%
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```\nclass Solution {\n    public long countAlternatingSubarrays(int[] arr) {\n        ArrayList<Long> count = new ArrayList<>();\n        long max = 1;\n        int x = arr[0];\n        int n = arr.length;\n        for(int i=1; i<n; i++){\n            if(arr[i]!=x){\n                max++;\n                x = arr[i];\n            }else{\n                count.add(max);\n                max = 1;\n                x = arr[i];\n            }\n        }\n        count.add(max);\n        \n        long ans = 0;\n        \n        for(int i=0; i<count.size(); i++){\n            long k = count.get(i);\n            ans = ans + ((k*(k+1))/2);\n        }\n        return ans;\n    }\n}\n```
['Java']