Most Intutive O(N^2) solution | C++ | Set | 10 lines of Code
Intuition\n Describe your first thoughts on how to solve this problem. \nFor every subarray [l...r] we just maintain an unordered set and when we add an element
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nFor every subarray [l...r] we just maintain an unordered set and when we add an element we just keep track of the no. of good pairs which can be easily done using unordered set or map kind of data structure.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nFor every subarray [l..r] when we add an element we just check the occurence of a[r] - 1 and a[r] + 1 and we can update the result further.\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n0(N*N) cause we have two for loops and assuming unordered_set does the operation in O(1).\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(N) as we have used an unordered set\n\n# Code \n```\nclass Solution {\npublic:\n    int sumImbalanceNumbers(vector<int>& nums) {\n        int n = nums.size();\n        int result=0;\n        unordered_set<int>st;\n        for(int l=0;l<n;l++){\n            unordered_set<int>st;\n            st.insert(nums[l]);\n            int good_pair=0;\n            for(int r=l+1;r<n;r++){\n                if(!st.count(nums[r])){\n                    good_pair += ( 1 - (st.count(nums[r]-1)>0) - (st.count(nums[r]+1)>0) );\n                }\n                result += good_pair;\n                st.insert(nums[r]);\n            }\n        }\n        return result;\n    }\n};\n```
['Ordered Set', 'C++']