Python | SortedList | O(n^2logn)
Code\n\nfrom sortedcontainers import SortedList\nclass Solution:\n    def sumImbalanceNumbers(self, nums: List[int]) -> int:\n        n = len(nums)\n        res
# Code\n```\nfrom sortedcontainers import SortedList\nclass Solution:\n    def sumImbalanceNumbers(self, nums: List[int]) -> int:\n        n = len(nums)\n        res = 0\n        for i in range(n):\n            sl = SortedList() \n            sl.add(nums[i])\n            tmpres = 0   \n            for j in range(i+1, n):\n                idx = sl.bisect_right(nums[j])\n                if idx:\n                    if idx == len(sl):\n                        if nums[j] - sl[-1] > 1:\n                            tmpres += 1\n                    else:\n                        if sl[idx] - sl[idx-1] > 1:\n                            if nums[j] - sl[idx-1] > 1 and sl[idx] - nums[j] > 1:\n                                tmpres += 1\n                            elif nums[j] - sl[idx-1] <= 1 and sl[idx] - nums[j] <= 1:\n                                tmpres -= 1\n                else:\n                    if sl[0] - nums[j] > 1:\n                        tmpres += 1\n                res += tmpres\n                sl.add(nums[j])\n        return res\n\n```
['Python3']