Some python solution.
Intuition\nIterate over all subarrays, use a set to check if adjacent elements exist in the prefix, adjust the number of imbalance and increase the answer. \n\n
# Intuition\nIterate over all subarrays, use a set to check if adjacent elements exist in the prefix, adjust the number of imbalance and increase the answer. \n\n# Approach\n\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n- O(n^2)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n- O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n    def sumImbalanceNumbers(self, nums: List[int]) -> int:\n        n = len(nums)\n        res = 0\n        for i in range(n):\n            prefix = set()\n            prefix.add(nums[i])\n            cur_min = nums[i]\n            cur_max = nums[i]\n            cur_imba = 0\n            for j in range(i + 1, n):\n                v = nums[j]\n                if v in prefix:\n                    pass\n                elif v > cur_max:\n                    cur_max = v\n                    if v - 1 not in prefix:\n                        cur_imba += 1\n\n                elif v < cur_min:\n                    cur_min = v\n                    if v + 1 not in prefix:\n                        cur_imba += 1\n                else:\n                    cur_imba += 1\n                    if v + 1 in prefix:\n                        cur_imba -= 1\n                    if v - 1 in prefix:\n                        cur_imba -= 1\n                \n                res += cur_imba\n                prefix.add(v)\n        \n        return res\n\n```
['Python3']