1st Kotlin Solution
Complexity\n- Time complexity: O(n)\n- Space complexity: O(1)\n\n# Code\n\nclass Solution {\n    fun countAlternatingSubarrays(nums: IntArray): Long {\n        
# Complexity\n- Time complexity: $$O(n)$$\n- Space complexity: $$O(1)$$\n\n# Code\n```\nclass Solution {\n    fun countAlternatingSubarrays(nums: IntArray): Long {\n        var result = 1L\n        var size = 1\n\n        for (i in 1..nums.size-1) {\n            size = if (nums[i-1] == nums[i]) 1 else size + 1\n            result += size\n        }\n\n        return result\n    }\n}\n```
['Kotlin']