aao solution samgho hindi me|| unique concept|| Bhut aasan h
Intuition\n Describe your first thoughts on how to solve this problem. \n# OR CODERS kya haal chaal h\nbhut hi aasan q puch liya \nmaine is question ko jab dekh
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n# OR CODERS kya haal chaal h\nbhut hi aasan q puch liya \nmaine is question ko jab dekha toh hume number of alternating subarrays nikalana h toh maine socha kyu n hum ye concept lagaye\n```\nconcept h\nnums = [0,1,1,1]\n- agar hume index=1 ko leke total subarrays index 1 tak hi \nkitne honge [1] [0,1] ye dono hi honge\nye humne kaise nikalna hoga, index--> 1-0+1=2 aise hi toh nikla hoga\n\n- agar hume index=2 ko leke total subarrays index 2 tak hi \nkitne honge [1] [0,1,1] [1,1] ye teeno hi honge\nye humne kaise nikalna hoga, index-->2-0+1=3 aise hi toh nikla hoga\n\n-ab maan lo hume index 2 se lekar index 1 se kitna subarray hoga\n[1] [1,1] yahi dono toh hoga, kaise nikala hoga \nindex-->2-1+1=2 aise hi toh nikala hoga\n```\ntoh formula nikla **index2-index1+1**\n\naao aaproach me dekhe kaise isko hum is question me kaise lagenyenge\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- humne i,j pointer liya dono 0 se start hua\n- humne check kiya ki pointer i and pointer j ke index pe same h toh ek hi subarray nikelga, ab starting wala index se ek hi toh banega.\n- fir humne j badha diya fir check kiya ki index j-1 and index j equal h kya h\n- agar equal h toh hum index j-1 ko nhi leke sakte subarray me yahi toh question tha ki same nhi lena, toh humne index i ko badha k j par hi kar diya or ab ek index se count me contribution ek hi toh hoga\n- agar equal nhi h toh hum formula laga k nikalenge ki kitna contribution hoga\n# aao example se samghe\n- Eg:- nums = [0,1,1,1]\n-  i=0,j=0, toh sirf index 0 kitna contribute karega ek hi subarray ([0]) toh hoga count=1;\n- fir humne jo aage badha diya j=1, or check kiya ki j-1 and j par value same h ya alag alag h. alga h toh formula lagya j-i+1=1-0+1=2\nindex 1 ko leke subarrays [0,1] [1] toh count=3 ho gya, humne j badha diya\n - j=2, humne dekha ki j-1 and j equal h toh hum j-1 ko subarray me nhi leke sakte toh humne i ko badha k j par hi akr diya or sirf akela j toh ek hi contribute karega count me, count=4, j ko aage badha diya \n - j=3 humne dekha ki j-1 and j equal h toh hum j-1 ko subarray me nhi leke sakte toh humne i ko badha k j par hi akr diya or sirf akela j toh ek hi contribute karega count me, count=5, j ko aage badha diya\n - toh total subarrays (count=5) 5 nikala\n\n**code dekh lete h**\n\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ --> O(N)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --> O(1)\n\n# Code\n```\nclass Solution {\npublic:\n    long long countAlternatingSubarrays(vector<int>& nums) {\n        long long n=nums.size();\n        long long i=0,j=0;\n        long long ans=0;\n        while(j<n)\n        {\n            //check kiya ki previous and current equal toh nhi\n            if(j!=0&&nums[j-1]!=nums[j])\n            {\n                formula lagya\n                long long add=(j-i+1);\n                ans=ans+add;\n            }\n            //equal h toh sirf 1 hi contribute karega\n            else\n            {\n                i=j;\n                long long add=1;\n                ans=ans+add;\n            }\n            j++;\n        }\n        return ans;\n    }\n};\n```\n![upvote.webp](https://assets.leetcode.com/users/images/ebaf6a1a-fda6-437b-97fa-4bcd669cfe54_1711866315.955602.webp)\n
['Two Pointers', 'C++']