0ms Beats100.00% slide window
IntuitionApproachComplexity
Time complexity:
O(n)
Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
$$O(n)$$
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```cpp []
class Solution {
public:
    long long countAlternatingSubarrays(vector<int>& nums) {
        long long ans = 0;
        int n = nums.size();
        int left = 0;

        for (int right = 0; right < n; right++) {
            if (right > 0 && nums[right] == nums[right - 1]) {
                left = right;
            }
            ans += (right - left + 1);
        }

        return ans;
    }
};

```
['C++']