Easy To Understand || Basic Understanding || C++ || Beats 90% || O(N*N)
Complexity\n- Time complexity: O(N*N)\n\n- Space complexity: O(1)\n\n# Code\n\nclass Solution {\npublic:\n    int gfat(int start,vector<int>& nums)\n    {\n    
# Complexity\n- Time complexity: O(N*N)\n\n- Space complexity: O(1)\n\n# Code\n```\nclass Solution {\npublic:\n    int gfat(int start,vector<int>& nums)\n    {\n        int sum=0;\n        int ans=0;\n        unordered_map<int,int>mp;\n        int maxi=nums[start];\n        int mini=nums[start];\n        mp[nums[start]]=1;\n        for(int j=start+1;j<nums.size();j++)\n        {\n            if(mp[nums[j]]==1)\n            {\n                sum+=ans;\n            }\n            else\n            {\n                if(nums[j]>maxi)\n                {\n                    if(mp[nums[j]-1]==0)\n                    {\n                        ans++;\n                    }\n                    maxi=nums[j];\n                    mp[nums[j]]=1;\n                }\n                else if(nums[j]<mini)\n                {\n                    if(mp[nums[j]+1]==0)\n                    {\n                        ans++;\n                    }\n                    mini=nums[j];\n                    mp[nums[j]]=1;\n                }\n                else\n                {\n                    bool f1=0;\n                    bool f2=0;\n                    if(mp[nums[j]-1]==1)\n                    {\n                        f1=1;\n                    }\n                    if(mp[nums[j]+1]==1)\n                    {\n                        f2=1;\n                    }\n                    if((f1==1)&&(f2==1))\n                    {\n                       ans--; \n                    }\n                    if((f1==0)&&(f2==0))\n                    {\n                       ans++;\n                    }\n                    mp[nums[j]]=1;\n                }\n                sum+=ans;\n            }\n        }\n        return sum;\n    }\n    int sumImbalanceNumbers(vector<int>& nums) \n    {\n       int count=0;\n       for(int i=0;i<nums.size();i++)\n       {\n           count+=gfat(i,nums);\n       }\n       return count;\n    }\n};\n```
['Counting', 'C++']