Easy 5 lines C++ Solution
Intuition\n Describe your first thoughts on how to solve this problem. \nYou are tasked with counting the number of alternating subarrays in an array, where alt
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nYou are tasked with counting the number of alternating subarrays in an array, where alternating subarrays are defined as subarrays whose adjacent elements alternate between two different values. The key insight is that if two adjacent elements alternate, any subarray formed by extending the alternating property contributes to the count.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1-Start from the second last element and check if it alternates with the next element.\n\n2-If they alternate, increment the current count curr, otherwise reset curr to 0.\n\n3-Add the current count plus 1 (since the current element itself forms a subarray) to the total sum sm.\n\n4-The total number of alternating subarrays is stored in sm.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nThe algorithm iterates over the array once, so the time complexity is \n\uD835\uDC42(\uD835\uDC5B) where n is the number of elements in the input array nums.\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nThe space complexity is \uD835\uDC42(1)since only a few extra variables (sm, curr, etc.) are used, regardless of the size of the input.\n\n# Dry Run\nLet\'s do a dry run on the test case nums = [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1] with your provided solution.\n\nInitialization:\nsm = 1 (since the last element always forms a valid subarray of length 1)\ncurr = 0\nn = 16 (length of the array)\nIteration (starting from i = n - 2 = 14 and going down to i = 0):\ni = 14:\n\nnums[14] = 0, nums[15] = 1\nThey alternate, so curr = 1 + curr = 1 + 0 = 1\nsm = sm + (1 + curr) = 1 + 1 + 1 = 3\ni = 13:\n\nnums[13] = 1, nums[14] = 0\nThey alternate, so curr = 1 + curr = 1 + 1 = 2\nsm = sm + (1 + curr) = 3 + 1 + 2 = 6\ni = 12:\n\nnums[12] = 0, nums[13] = 1\nThey alternate, so curr = 1 + curr = 1 + 2 = 3\nsm = sm + (1 + curr) = 6 + 1 + 3 = 10\ni = 11:\n\nnums[11] = 1, nums[12] = 0\nThey alternate, so curr = 1 + curr = 1 + 3 = 4\nsm = sm + (1 + curr) = 10 + 1 + 4 = 15\ni = 10:\n\nnums[10] = 0, nums[11] = 1\nThey alternate, so curr = 1 + curr = 1 + 4 = 5\nsm = sm + (1 + curr) = 15 + 1 + 5 = 21\ni = 9:\n\nnums[9] = 0, nums[10] = 0\nThey don\'t alternate, so curr = 0\nsm = sm + (1 + curr) = 21 + 1 + 0 = 22\ni = 8:\n\nnums[8] = 1, nums[9] = 0\nThey alternate, so curr = 1 + curr = 1 + 0 = 1\nsm = sm + (1 + curr) = 22 + 1 + 1 = 24\ni = 7:\n\nnums[7] = 0, nums[8] = 1\nThey alternate, so curr = 1 + curr = 1 + 1 = 2\nsm = sm + (1 + curr) = 24 + 1 + 2 = 27\ni = 6:\n\nnums[6] = 1, nums[7] = 0\nThey alternate, so curr = 1 + curr = 1 + 2 = 3\nsm = sm + (1 + curr) = 27 + 1 + 3 = 31\ni = 5:\n\nnums[5] = 0, nums[6] = 1\nThey alternate, so curr = 1 + curr = 1 + 3 = 4\nsm = sm + (1 + curr) = 31 + 1 + 4 = 36\ni = 4:\n\nnums[4] = 1, nums[5] = 0\nThey alternate, so curr = 1 + curr = 1 + 4 = 5\nsm = sm + (1 + curr) = 36 + 1 + 5 = 42\ni = 3:\n\nnums[3] = 0, nums[4] = 1\nThey alternate, so curr = 1 + curr = 1 + 5 = 6\nsm = sm + (1 + curr) = 42 + 1 + 6 = 49\ni = 2:\n\nnums[2] = 1, nums[3] = 0\nThey alternate, so curr = 1 + curr = 1 + 6 = 7\nsm = sm + (1 + curr) = 49 + 1 + 7 = 57\ni = 1:\n\nnums[1] = 0, nums[2] = 1\nThey alternate, so curr = 1 + curr = 1 + 7 = 8\nsm = sm + (1 + curr) = 57 + 1 + 8 = 66\ni = 0:\n\nnums[0] = 1, nums[1] = 0\nThey alternate, so curr = 1 + curr = 1 + 8 = 9\nsm = sm + (1 + curr) = 66 + 1 + 9 = 76\nFinal Result:\nThe total sum of alternating subarrays is 76.\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    long long countAlternatingSubarrays(vector<int>& nums) {\n        long long sm = 1, curr = 0;\n        int n  = nums.size();\n        for(int i=n-2;i>=0;i--){\n            if(nums[i] != nums[i+1])curr = 1+curr;\n            else curr = 0;\n            sm += (1+curr);\n        }\n        return sm;\n    }\n};\n```
['Array', 'Math', 'C++']