Easy and basic approach||O(n)||Beats 98%
Intuition\n\nIntuition:\nThe code aims to count the number of alternating subarrays within the given array nums. An alternating subarray is defined as a contigu
# Intuition\n\nIntuition:\nThe code aims to count the number of alternating subarrays within the given array nums. An alternating subarray is defined as a contiguous subarray where adjacent elements have different values.\n\n# Approach\nInitialize variables right and left to track the boundaries of the current subarray and initialize ans to 0 to store the count of alternating subarrays.\nIterate through the array using a while loop until right reaches the second last element (n-1).\nWithin the loop, check if the current element (nums[right]) is different from the next element (nums[right+1]). If they are different, extend the current subarray by moving right pointer one step forward.\nIf the current element is equal to the next element, it indicates the end of the alternating subarray. Update left to be the index of the next element (right+1) and move right pointer one step forward.\nFor each iteration of the loop, update the count of alternating subarrays by adding the length of the current subarray (right - left + 1) to ans.\nFinally, return ans + 1 to account for the case when the whole array itself is an alternating subarray.\n\n# Complexity\n- Time complexity:\nThe while loop iterates through the array once, and within each iteration, constant-time operations are performed. Therefore, the time complexity is O(n), where n is the length of the input array nums.\n\n- Space complexity:\n The space complexity is O(1) because the algorithm uses only a constant amount of extra space regardless of the size of the input array.\n\n# Code\n```\nclass Solution {\n    public long countAlternatingSubarrays(int[] nums) {\n        int n=nums.length;\n        int right=0;\n        int left=0;\n        long ans=0;\n        while(right<n-1){\n            if(nums[right]!=nums[right+1]){\n           right++;\n            }else{\n                 left=right+1;\n                 right++;\n            }\n         ans+=right-left+1;   \n        }\n        return ans+1;\n    }\n}\n```
['Java']