Python 3 || 9 lines,  w/ explanation ||   T/S: 63% / 80%
Here\'s how the code works:\n\nWe iterate overnumsin an outer loop, initializingsto keep track of distinct elements encountered, andcntto count the imbalance nu
Here\'s how the code works:\n\nWe iterate over`nums`in an outer loop, initializing`s`to keep track of distinct elements encountered, and`cnt`to count the *imbalance* numbers.\n\nWe also iterate over an inner loop, checking whether the element`num2`has been seen. If not, we add `num2`to`s`.\n\nWe  check (a) whether both`num2-1`and`num2+1`are present in `s`(in which case we decrement`cnt`) or (b) whether both``num2-1``and`num2+1`are not present in `s`(in which case we increment`cnt`).\n\nFinally, we add the value of`cnt`to`ans`after each execution of the inner loop, and return`ans`after the execution of the outer loop.  -- *ChatGPT*\n\n```\nclass Solution:\n    def sumImbalanceNumbers(self, nums: List[int]) -> int:\n\n        n, ans = len(nums), 0\n\n        for i, num1 in enumerate(nums):\n\n            s, cnt  = {num1}, 0\n\n            for num2 in nums[i+1:]:\n\n                if num2 not in s:\n                    s.add(num2)\n                    cnt+= 1 - (num2-1 in s) - (num2+1 in s)\n\n                ans+= cnt\n                \n        return ans\n```\n[https://leetcode.com/problems/sum-of-imbalance-numbers-of-all-subarrays/submissions/1296014827/](https://leetcode.com/problems/sum-of-imbalance-numbers-of-all-subarrays/submissions/1296014827/)\n\nI could be wrong, but I think that time complexity is *O*(*N*^2) and space complexity is *O*(*N*), in which *N* ~`len(nums)`.
['Python3']