Sorted Set (TreeSet) approach | Java solution | Clean Code
Complexity\n- Time complexity: $O(n^2 \cdot \log n)$\n Add your time complexity here, e.g. O(n) \n\n- Space complexity: $O(n)$\n Add your space complexity here,
# Complexity\n- Time complexity: $O(n^2 \\cdot \\log n)$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $O(n)$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java\nclass Solution {\n    public int sumImbalanceNumbers(int[] nums) {\n        int ans = 0;\n        for(int i = 0;i < nums.length;i++){\n            int curr = 0;\n            TreeSet<Integer> st = new TreeSet<>();\n            for(int j = i;j < nums.length;j++){\n                var l = st.floor(nums[j]);\n                var h = st.ceiling(nums[j]);\n                if(l != null && h != null){\n                    if(h - l > 1 && curr > 0) --curr;\n                    if(nums[j] - l > 1) ++curr;\n                    if(h - nums[j] > 1) ++curr;\n                }\n                else if(l != null){\n                    if(nums[j] - l > 1) ++curr;\n                }\n                else if(h != null){\n                    if(h - nums[j] > 1) ++curr;\n                }\n                ans += curr;\n                st.add(nums[j]);\n            }\n        }\n        return ans;\n    }\n}\n```
['Ordered Set', 'Java']