C++ | Multiset🔥
Set can be used for this approach.\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 Ad
###### Set can be used for this approach.\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    {\n        int ans = 0, n = nums.size();\n        for (int i = 0; i < n; i++)\n        {\n            multiset<int> ms;\n            int cnt = 0;\n            \n            for (int j = i; j < n; j++)\n            {\n                if (ms.find(nums[j]) != ms.end())\n                {\n                    ans += cnt;\n                    continue;\n                }\n\n                auto itr = ms.insert(nums[j]);\n                auto next = itr;\n                auto prev = itr;\n\n                next++;\n                prev--;\n\n                if (next != ms.end() and itr != ms.begin())\n                {\n                    if ((*next) - (*prev) > 1)\n                        cnt--;\n\n                    if ((*next) - nums[j] > 1)\n                        cnt++;\n\n                    if (nums[j] - (*prev) > 1)\n                        cnt++;\n                }\n                else if (next != ms.end())\n                {\n                    if ((*next) - nums[j] > 1)\n                        cnt++;\n                }\n                else if (itr != ms.begin())\n                {\n                    if (nums[j] - (*prev) > 1)\n                        cnt++;\n                }\n\n                ans += cnt;\n            }\n        }\n        return ans;\n    }\n};\n```
['Binary Search', 'C++']