O(1) space and O(n) time - DP Approach Java
IntuitionApproachDp approach using two variablesComplexity
Time complexity: O(n)

Space complexity: O(1)
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->
Dp approach using two variables

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


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

# Code
```java []
class Solution {
    public long countAlternatingSubarrays(int[] nums) {
        int n = nums.length, incLength = 1;
        long  maxLength = 1;
        
        for(int i=1; i<n; i++){
            incLength =  (nums[i-1]!=nums[i]) ? incLength+1 : 1;
            maxLength += incLength;       
        }
        return maxLength;        
    }
}
```
['Dynamic Programming', 'Java']