Beginner Friendly || Maps || C++ || Easy to Understand || Beats 90%
Intuition\n Describe your first thoughts on how to solve this problem. \nUsually in Subarray problems some kind of two pointer shenanigans are there, here we ge
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nUsually in Subarray problems some kind of two pointer shenanigans are there, here we get all subarrays using two loops.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nWe run two loops i=0->n and j=i->n this will give us all the subarrays. Now Let\'s say we have a **number X **.\n\n**if X is present in array beforehand we dont need to do anything**\n\n**if X-1 and X+1 are not present that signifies that this element creates imbalance so we increase imbalance (curr in code) by 1**\n\n**if X-1 is present and X+1 is present this means that there was imbalance beforehand now we need to reduce it.\nEg: {1,3} has 1 imbalance but {1,3,2} has 0.**\n\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->O(n^2)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --> O(n)\n\n# Code\n```\nclass Solution {\npublic:\n    int sumImbalanceNumbers(vector<int>& nums) {\n        int n=nums.size();\n        \n        if(n==1) return 0;\n        int ans=0;\n\n        for(int i=0;i<n;i++){\n\n           unordered_map<int,int>mp;\n           int curr=0;\n\n            for(int j=i;j<n;j++){\n                int x=nums[j];\n                if(mp.find(x)!=mp.end()){}\n\n                else if(mp.find(x-1)!=mp.end() && mp.find(x+1)!=mp.end()) curr--;\n\n                else if(mp.find(x+1)==mp.end() && mp.find(x-1)==mp.end() && mp.size()!=0){\n                    curr++;\n                }\n                mp[x]=1;    \n                ans+=curr;\n            }\n            \n            \n        }\n        return ans;\n    }\n};\n```
['C++']