C++ Solution Using Set
Intuition\n- The naive solution is to create all the subarrays and then check whether the current subarray has imbalance or not. This would result in the time c
# Intuition\n- The naive solution is to create all the subarrays and then check whether the current subarray has imbalance or not. This would result in the time complexity be O(n^3) which goes out of our required Time Complexity\n\n    So we need to do this in O((N^2)* logN) atmost\n- Here we can used Set DS to check whether on adding of our element our current subarray has become imbalance or it has become balance\n\n# Approach\n- Consider if an array as imbalance ex 3 5 ... so on addding 4 the subarray becomes balance \n- So on adding an element the current subarray has reduced its imbalance by a factor of 1 (as there can be many other imbalances in our current subarray) if element - 1 and element + 1 exists in the array\n- If our current element is already present then no change as this is also an imbalanced array as preivious\n- If only one element i.e. element-1 or element+1 is present in the subarray then the array is imbalanced\n\nNow we increment our counter for each subarray that has imbalance and how many times \n\n# Code\n```\nclass Solution {\npublic:\n    int sumImbalanceNumbers(vector<int>& nums) {\n        int n = nums.size();\n\n        int count = 0 ;\n        for(int i=0;i<n;i++){\n            set<int> s;\n            s.insert(nums[i]);\n            int imbalances = 0;     // checking all subarrys and initially only single element presnent and hence no imbalance there\n            for(int j=i+1;j<n;j++){\n                if(s.find(nums[j])==s.end()){       // if current element does not exists\n                    if(s.find(nums[j]-1)!=s.end() && s.find(nums[j]+1)!=s.end())   // if element -1 and element+1 exists then \n                       imbalances--;                                             // number of imbalances gets decreased by one\n                    else if(s.find(nums[j]-1)==s.end() && s.find(nums[j]+1)==s.end()) // if either of the element-1 or element+1 is \n                        imbalances++;                                        // present then imbalance increases by a factor of 1\n                }\n                s.insert(nums[j]);  \n                count+=imbalances;      // increment the count by the imbalances present in the current subarray\n            }\n        }\n\n        return count;\n    }\n};\n```
['C++']