C++ Set + Sliding Window + Binary Search
Approach\n Describe your approach to solving the problem. \nMaintain a set in each sliding window to see if the new elements could create more imbalance number 
# Approach\n<!-- Describe your approach to solving the problem. -->\nMaintain a set in each sliding window to see if the new elements could create more imbalance number or less.\n# Complexity\n- Time complexity: O(n^2 logn)\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            set<int> sortedS {nums[i]};\n            int count = 0;\n            for (int j = i + 1; j < nums.size(); j++) {\n                if (!sortedS.count(nums[j])) {\n                    auto it = sortedS.lower_bound(nums[j]);\n                    if (it == sortedS.end()) {\n                        if (nums[j] - *(sortedS.rbegin()) > 1) {\n                            count++;\n                        }\n                    }\n                    else if (it == sortedS.begin()) {\n                        if (*it - nums[j] > 1) {\n                            count++;  \n                        }         \n                    }\n                    else { // always *it - *lastI > 1\n                        auto lastI = prev(it);\n                        if (*it - nums[j] > 1 && nums[j] - *lastI > 1) {\n                            count++;\n                        }\n                        else if (*it - nums[j] <= 1 && nums[j] - *lastI <= 1) {\n                            count--;\n                        }\n                    }\n                }\n                ans += count;\n                sortedS.insert(nums[j]);\n            }\n        }\n        return ans;\n    }\n};\n```
['C++']