Easy to understand: O(n) two pointer & cool math trick approach EXPLAINED
IntuitionAt first, I utilized brute force to solve this problem, where I went through each of the subarrays and checked if they were valid, and if they were, I
# Intuition
At first, I utilized brute force to solve this problem, where I went through each of the subarrays and checked if they were valid, and if they were, I added them to my "res" variable, which represents result and is what is ultimately returned. However, while this approach worked in principle, it exceeded the alloted time restrictions for this problem. So, this got me thinking about the repeated work that was being done in the brute force solution: this repeated work occured when we have already established that a larger subarray is valid, and then we have to check if a subarray within that larger subarray is valid later. Obviously, that smaller subarray within the larger subarray will be valid due to the larger subarray being valid. To clarify, here is an example: larger valid subarray-- [1,0,1,0]. Smaller subarray-- [1,0,1]. It is clear that the smaller subarray is valid because we know alreday the larger subarray to be valid and the smaller subarray is entirely inside the larger subarray. With this understanding of the repeated work, I started to work on how I could solve this issue and ultimately came to the following solution.

# Approach
I started by establishing left and right pointers, and instead of iterating through and checking that each subarray was valid (like I did in the brute force solution), so long as a subarray continued to be valid, I continued to extend it by incrementing my right pointer. Finally, when the subarray was no longer valid, I would add (length of subarray)(length of subarray + 1) // 2 to my result. How did I come to this obscure formula? Well, it was through recognition of a very simple pattern that is better explained through example: Let's say we have a subarray [1], we know that there is only one subarray within that subarray, so we would have an output of one. Let's say we have a subarray [1,0]. We know that we would have one subarray of length 2 ([1,0]) and two subarrays of length 1 ([1], [0]), so the output for a subarray of length 2 would be 3. Let's say we have a subarray [1,0,1]. We know that we would have one subarray of length 3 ([1,0,1]), two subarrays of length 2 ([1,0], [0,1]), and three subarrays of legnth 1 ([1], [0], [1]), so for a subarray of length 3, our output would be 6. Following this relation, the mapping of input subarray sizes to their outputs is as follows: 1:1, 2:3, 3:6, 4:10, 5:15. Now that we have the outputs listed, we can notice the following patttern. input n*input n + 1 / 2 is the output for n. ie: (4 * 5 / 2 = 10). Now, we know that when we find the longest valid subarray starting from our left pointer, we can add to "res" all of the valid subarrays within it! 

# Complexity
- Time complexity:
O(n)-- iterating through the nums array once

- Space complexity:
O(1)-- res, l pointer, r pointer.

# Code
```python3 []
class Solution:
    def countAlternatingSubarrays(self, nums: List[int]) -> int:
        res = 0
        l = 0
        r = l

        while l < len(nums):
            if l == r or (r < len(nums) and nums[r] != nums[r - 1]):
                r += 1
                continue
            if r == len(nums) or nums[r] == nums[r - 1]:
                res += ((r - l + 1)*(r - l)) // 2
                l = r
            
                
        return res

        
```
['Python3']