C++ Solution
\nclass Solution {\npublic:\n    int sumImbalanceNumbers(vector<int>& nums) {\n        int n = nums.size();\n        int sol = 0;\n        for (int i = 0; i < n
```\nclass Solution {\npublic:\n    int sumImbalanceNumbers(vector<int>& nums) {\n        int n = nums.size();\n        int sol = 0;\n        for (int i = 0; i < n; i++) {\n            int count = 0;\n            set<int> st;\n            st.insert(nums[i]);\n            for (int j = i + 1; j < n; j++) {\n                if (st.count(nums[j]) == 0) {\n                    auto it = st.lower_bound(nums[j]);\n                    if (it == st.begin()) {\n                       if (*it - nums[j] > 1) ++count;\n                    }\n                    if (it == st.end()) {\n                        auto pr = prev(it);\n                        if (nums[j] - *pr > 1) ++count;\n                    }\n                    if (it != st.begin() && it != st.end()) {\n                        auto it1 = prev(it);\n                        if (*it1 + 1 < nums[j] && nums[j] + 1 < *it) ++count;\n                        if (*it1 + 1 == nums[j] && nums[j] + 1 == *it) --count;\n                    }\n                }\n                st.insert(nums[j]);\n                sol += count;\n            }\n        }\n        return sol;\n    }\n};\n```
[]