👌Commented line👌||O(n^2) Solution || Easy code || using set // understandable approach ➡️
Approach\n1) Iterate through each element (num) in the input vector (nums) using an outer loop.\n2) Initialize an empty set (st) to store the previously encount
# Approach\n1) Iterate through each element (num) in the input vector (nums) using an outer loop.\n2) Initialize an empty set (st) to store the previously encountered numbers for each iteration.\n3) Initialize a counter variable (cnt) to 0.\n4) Iterate through the elements from the current position (i) to the end of the vector using an inner loop.\n5) Check if the current number (nums[j]) is not present in the set (st).\n     If it is not present, check if both the incremented value (nums  [j]+1) and decremented value (nums[j]-1) are present in the set.\nIf both are present, decrement the counter (cnt) by 1.\nIf both are not present, increment the counter (cnt) by 1.\n6) Insert the current number (nums[j]) into the set (st).\n7) Add the value of the counter (cnt) to the answer (ans).\n\n# Complexity\n- Time complexity:\n O(n^2)\n\n- Space complexity:\n O(n)\n\n# Code\n```\nclass Solution {\npublic:\n    int sumImbalanceNumbers(vector<int>& nums) {\n        int n=nums.size();\n        int ans=0;\n        for(int i=0;i<n;i++)\n        {\n            unordered_set<int>st;\n            int cnt=0;\n            for(int j=i;j<n;j++)\n            {\n             if(!st.count(nums[j])) // if not present in the set\n            {\n        if(st.count(nums[j]+1) && st.count(nums[j]-1)) // +1,-1 presnt         \n                {\n                    cnt--; \n                }\n         // if +1 ,-1 both not present in the set       \n      else if(st.size()>0 && !st.count(nums[j]+1) && !st.count(nums[j]-1))\n                cnt++;\n            }\n             st.insert(nums[j]);\n             ans+=cnt;\n\n            }\n        }\n        return ans;\n    }\n};\n```
['Ordered Set', 'C++']