[c++] Using set to modify imbalance number
Code\n\nclass Solution {\npublic:\n    int sumImbalanceNumbers(vector<int>& nums) {\n        int n=nums.size();\n        int ans=0;\n            \n        for(i
# Code\n```\nclass Solution {\npublic:\n    int sumImbalanceNumbers(vector<int>& nums) {\n        int n=nums.size();\n        int ans=0;\n            \n        for(int i=0;i<n-1;i++){\n            set<int> st;\n            st.insert(nums[i]);\n            st.insert(nums[i+1]);\n            \n            int mx=max(nums[i],nums[i+1]);\n            int mn=min(nums[i],nums[i+1]);\n            \n            int imb_num=(mx-mn>1);\n            ans+=imb_num;\n            for(int j=i+2;j<n;j++){\n                if(st.count(nums[j])){\n                    ans+=imb_num;\n                    continue;\n                }\n                st.insert(nums[j]);\n                \n                auto it=st.find(nums[j]);\n                auto prev=it,next=it;\n                auto end=st.end();\n                end--;\n                \n                prev--;next++;\n                if(it==st.begin()){\n                    int k=*next;\n                    imb_num+=(k-nums[j]>1);\n                }\n                else if(it==end){\n                    int k=*prev;\n                    imb_num+=(nums[j]-k>1);\n                }\n                else{\n                    int a=*prev,b=*it,c=*next;\n                    imb_num+=(b-a>1)+(c-b>1)-(c-a>1);\n                }\n                \n                ans+=imb_num;\n            }\n            \n        }\n        \n        return ans;\n    }\n};\n```
['C++']