Fully Explained || C++
Intuition\n Describe your first thoughts on how to solve this problem. You can see that n^2 TC is allowed hence you can check for all subarrays , hence now you 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->You can see that n^2 TC is allowed hence you can check for all subarrays , hence now you just need to find out how adding a number would change the precalculated value of imbalance.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nif you add a number in a subset , in ordered set the number could come at three positions-:\nprecalculated value=found;\na) at beginning (if(a[1]-a[0]>1) found++);\nb) at end (if(a[end]-a[end-1])>1 found++);\nc) in middle (if(a[mid]-a[mid-1]>1 found++)\n             if(a[mid+1]-a[mid])>1 found++,\n             if(a[mid+1]-a[mid-1])>1 found--);\nthat\'s all you need to do , for each subarray.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(n^2.log(n));\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    int sumImbalanceNumbers(vector<int>& nums) {\n        int ans=0;\n        for(int i=0;i<nums.size();i++){\n            set<int> st;\n            map<int,int> mp;\n            int found=0;\n            //cout<<1<<"22"<<endl;\n            for(int j=i;j<nums.size();j++){\n                mp[nums[j]]++;\n                if(mp[nums[j]]>1){\n                    ans+=found;\n                    continue;\n                }\n                st.insert(nums[j]);\n                //cout<<1<<endl;\n                if(st.size()==1){\n                    continue;\n                }\n\n                auto it=st.lower_bound(nums[j]);\n                if(it==st.begin()){\n                    it++;\n                    int val=(*it);\n                    if(val-nums[j]>1){\n                       found++;\n                    }\n                    ans+=found;\n                   //cout<<val<<"1"<<endl;\n                    continue;\n                }\n                it++;\n                if(it==st.end()){\n                    it--;\n                    it--;\n                    int val=(*it);\n                    if(nums[j]-val>1){\n                        found++;\n                    }\n                    //cout<<val<<"2"<<endl;\n                }\n                else{\n                    int val1=(*it);\n                    it--;\n                    it--;\n                    int val2=(*it);\n                    if(nums[j]-val2>1){\n                        found++;\n                    }\n                    if(val1-nums[j]>1){\n                        found++;\n                    }\n                    if(val1-val2>1){\n                        found--;\n                    }\n                    //cout<<val1<<" "<<val2<<"3"<<endl;\n                }\n            //cout<<found<<endl;\n            ans+=found;                \n            }\n\n        }\n        return ans;\n    }\n};\n```
['Ordered Set', 'C++']