Using multiset + Understandable Code | C++
Intuition\n Describe your first thoughts on how to solve this problem. \nAs we have to mainatin sorted order of each subarray multiset can be used\n# Approach\n
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nAs we have to mainatin sorted order of each subarray multiset can be used\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n\n# Complexity\n- Time complexity:$O(n^2logn)$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:$O(n)$\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            multiset<int>s;\n            s.insert(nums[i]);\n            int imb=0;\n            for(int j=i+1;j<nums.size();j++){\n                int same=s.count(nums[j]);\n                int lower=s.count(nums[j]-1);\n                int upper=s.count(nums[j]+1);\n                \n                if(same==0 && upper==0 && lower==0){\n                    imb++;\n                    ans+=imb;\n                }\n                else if(same==0 && upper>=1 && lower>=1){\n                    imb--;\n                    ans+=imb;\n                }\n                else if(same>=1 || lower>=1 || upper>=1){\n                     ans+=imb;\n                }\n\n                s.insert(nums[j]);\n            }\n        }\n        return ans;\n    }\n};\n```
['C++']