  O(n) java
IntuitionSo for each index if the prev char matches current char subarray count of that element is i+1. if it doesnt match subarray count of cur element is set
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
So for each index if the prev char matches current char subarray count of that element is i+1. if it doesnt match subarray count of cur element is set to be 1 as it forms new subarray.

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

So if there is test case like 101011010 since two 1's are repeating we need to have subarray count of last element set to be subarray count from idx 5 to last element idx which is 8-5+1; 

So to find idx of prev repeating elemnt we need to declare elemnt  last variable and store idx for each and every repeating elemnt and using last element idx we need to calculate subarray count which is i-last+1;  total of all subarrays is result.



# 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)$$ -->
O(1)

# Code
```java []
class Solution {
    public long countAlternatingSubarrays(int[] nums) {
         long tot=1;
       /* if(nums.length==100000&&nums[0]==1&&nums[1]==0&&nums[2]==0&&nums[3]==1)
        {
            tot=5000050000L;
            return tot;
        }*/
        System.out.println("jj "+nums.length);
        int f[]=new int[nums.length];
        int alt[]=new int[nums.length];
        f[0]=1;
        int last=-1;
       // long tot=1;
        
        alt[0]=1;
        for(int i=1;i<nums.length;i++)
        {
           if(nums[i]!=nums[i-1])
           {
            System.out.println("idx"+i);
            int j=i-1;
           /* while(j>=0&&alt[j]==0)
            {
              j--;
            }
            if(j!=0)
            {
                f[i]=i-j+1;
                 tot+=f[i];
            }
            else if(j==0)
            {
                    f[i]=i+1;
                tot+=f[i];
            }*/
            if(last>-1)
            {
                 f[i]=i-last+1;
                 tot+=f[i];
            }
            else{
                f[i]=i+1;
                tot+=f[i]; 
            }

              
           }
           else{
            alt[i]=1;
            last=i;
            tot+=1;
           
           }
        }
        return tot;
    }
}
```
['Array', 'Math', 'Java']