Multiset prev and next iterators
Code
# Code
```cpp []
class Solution {
public:
    int sumImbalanceNumbers(vector<int>& nums) {
        int n = nums.size(), ans = 0;
        for (int i = 0; i < n; i++) {
            multiset<int> st;
            st.insert(nums[i]);
            int ptr = 0;
            for (int j = i + 1; j < n; j++) {
                auto ub = st.upper_bound(nums[j]);
                if (ub == st.end()) {
                    auto it1 = prev(ub);
                    ptr += ((nums[j] - *it1) > 1);
                } else if (ub == st.begin())
                    ptr += ((*ub - nums[j]) > 1);
                else {
                    auto it1 = prev(ub);
                    ptr -= ((*ub - *it1) > 1);
                    ptr += ((*ub - nums[j]) > 1);
                    ptr += ((nums[j] - *it1) > 1);
                }
                st.insert(nums[j]);
                ans += ptr;
            }
        }
        return ans;
    }
};
```
['C++']