[C++] Easy Solution
Intuition\r\n Describe your first thoughts on how to solve this problem. \r\nView my video solution\r\nhttps://youtu.be/EUN-nCXJBAw\r\n\r\n\r\n# Code\r\n\r\ncla
# Intuition\r\n<!-- Describe your first thoughts on how to solve this problem. -->\r\nView my video solution\r\nhttps://youtu.be/EUN-nCXJBAw\r\n\r\n\r\n# Code\r\n```\r\nclass Solution {\r\npublic:\r\n    int sumImbalanceNumbers(vector<int>& nums) {\r\n        int ans=0;\r\n        for(int i=0;i<nums.size();i++){\r\n            set<int> s;\r\n            int c=0;\r\n            for(int j=i;j<nums.size();j++){\r\n                if(s.find(nums[j]) != s.end()){ans+=c; continue;}\r\n                 s.insert(nums[j]);\r\n               \r\n                auto it = s.find(nums[j]);\r\n                auto nxt = next(it);\r\n                if(it != s.begin() && nxt !=s.end()){\r\n                 auto x = prev(it);\r\n                    if(*it - *x >1 && *nxt - *it>1) c++;\r\n                    if(*it - *x<=1 && *nxt - *it<=1) c--;\r\n                    \r\n                }\r\n                if(it == s.begin() && nxt!=s.end()){\r\n                    if(*nxt - *it >1)c++;\r\n                }\r\n                \r\n                if(it!=s.begin() && nxt == s.end()){\r\n                    auto x = prev(it);\r\n                    if( *it - *x>1) c++;\r\n                }\r\n                \r\n            ans+=c;\r\n            }\r\n        }\r\n        return ans;\r\n    }\r\n};\r\n\r\n// 1 4 7 -> 2\r\n    \r\n// x 1 4 7 -> \r\n\r\n// 1 4 7 y -> \r\n\r\n// 1 4 c 7 -> \r\n    \r\n// 1 4 4 7\r\n\r\n\r\n```
['C++']