C++ Solution || Easy to understand
\n\n# Code\n\nclass Solution {\npublic:\n    int sumImbalanceNumbers(vector<int>& nums) {\n        int ans=0; //total count of imbalance numbers\n        for(in
\n\n# Code\n```\nclass Solution {\npublic:\n    int sumImbalanceNumbers(vector<int>& nums) {\n        int ans=0; //total count of imbalance numbers\n        for(int i=0; i<nums.size(); i++){\n            int t=0; //count of imbalance numbers of subarrays starting from ith position\n            vector<int> vis(1005, 0); //an array to keep track of which number is visited or not in the subarray\n            vis[nums[i]]=1;\n            for(int j=i+1; j<nums.size(); j++){\n                if(vis[nums[j]]==1){\n                    //nums[j] is already present in visited array\n                    ans+=t;\n                    continue;\n                }\n                vis[nums[j]]=1;\n                if(vis[nums[j]+1]==1 && vis[nums[j]-1]==1){\n                    //if nums[j]+1 and nums[j]-1 are also present in visited array \n                    t--; // when nums[j] was not present, then \'t\' was containing the count of the pair (nums[j]+1 and nums[j]-1), \n                        //now that nums[j] is present we have to decrement the current count of imbalance numbers(t) \n                    ans+=t;\n                    continue;\n                }  \n                if(vis[nums[j]+1]==1 || vis[nums[j]-1]==1){\n                    //if only one of the value nums[j]+1 or nums[j]-1 is present, then the current count of imbalance numbers does not change\n                    ans+=t;\n\n                }  \n                else{\n                    t++;\n                    ans+=t;\n                }\n            }\n        }\n        return ans;\n    }\n};\n```
['C++']