[Python 3] Hash set to update connectivity
Intuition\nO(n^2) TC. Loop all possible starting indices and increase subarray element by element to the last index. For each starting index, keep a hashset to 
# Intuition\nO(n^2) TC. Loop all possible starting indices and increase subarray element by element to the last index. For each starting index, keep a hashset to check whether a new number will introduce imbalances, or reduce them. Then add the new number to hashset, and add updated imbalances to answer.\n\n# Code\n```\nclass Solution:\n    def sumImbalanceNumbers(self, nums: List[int]) -> int:\n        n = len(nums)\n        ans = 0\n        \n        for i in range(n - 1):\n            st = set([nums[i]])\n            mi = ma = nums[i]\n            imb = 0\n            for j in range(i + 1, n):\n                if nums[j] in st:\n                    ans += imb\n                    continue\n                if nums[j] < mi:\n                    mi = nums[j]\n                    if nums[j] + 1 not in st:\n                        imb += 1\n                elif nums[j] > ma:\n                    ma = nums[j]\n                    if nums[j] - 1 not in st:\n                        imb += 1\n                elif nums[j] + 1 in st and nums[j] - 1 in st:\n                    imb -= 1\n                # elif (nums[j] + 1 in st and nums[j] - 1 not in st) or \\\n                #    (nums[j] + 1 not in st and nums[j] - 1 in st):\n                elif nums[j] + 1 not in st and nums[j] - 1 not in st:\n                    imb += 1\n                ans += imb\n                st.add(nums[j])\n        return ans\n                    \n                    \n```
['Python3']