C++ | Fast and Easy to Understand
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    int sumImbalanceNumbers(vector<int>& nums) {\n        //O(n^2log(n))\n        int n = nums.size();\n        int res = 0;\n        for(int i = 0; i < n-1; ++i){\n            set<int> s;\n            int mini = nums[i], maxi = nums[i];\n            s.insert(nums[i]);\n            int count = 0;\n            for(int j = i+1; j < n; ++j){\n                if(s.find(nums[j]) == s.end()){\n                    s.insert(nums[j]);\n                    auto it = s.find(nums[j]), it1 = s.find(nums[j]);\n                    if(it != s.begin()){\n                        if(*(it) - *(--it1) >= 2) count++;    \n                    }\n                    it1 = it;\n                    if(it != (--s.end())){\n                        if(*(++it1) - *(it) >= 2) count++;\n                    }\n                    it1 = it;\n                    if(it != s.begin() && it != (--s.end())){\n                        if(*(++it1) - *(--it) >= 2) count--;\n                    }\n                }\n                res += count;\n                s.insert(nums[j]);\n            }\n        }\n        return res;\n    }\n};\n```
['C++']