java orderedSet
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public int sumImbalanceNumbers(int[] nums) {\n\n        // 1. nums[i] \uB97C 0~n-1\uAE4C\uC9C0 \uC21C\uD68C\uD558\uBA74\uC11C \n        //    nums[i]\uB97C \uB9C8\uC9C0\uB9C9 \uC6D0\uC18C\uB85C \uD558\uB294 subString\uC744 \uB9CC\uB4E4\uC5B4\uB098\uAC00\uBA74\uC11C \n        //    List<Integer> list \uC5D0 \uC815\uB82C\uD574\uC11C \uB123\uC5B4\uC900\uB2E4. \n        //    \u203B count\uB97C \uD558\uB098 \uC120\uC5B8\uD574\uC11C imbalance \uAC1C\uC218\uB97C \uC720\uC9C0\uD574\uC900\uB2E4.\n        //    list\uC5D0 \uB123\uC744 \uB54C \n        //    1) \uB123\uC73C\uB824\uB294 \uACF3\uC5D0 \uC55E\uC5D0 \uC6D0\uC18C(idx-1), \uB4A4\uC5D0 \uC6D0\uC18C(idx+1)\uAC00 \uC874\uC7AC\uD558\uBA74 \n        //        list.get(idx-1) ,list.get(idx+1) \n        //       \uB450\uAC1C\uB294 \uD604\uC7AC imbalance\uB85C count\uAC00 \uB41C \uC0C1\uD0DC\uC774\uBBC0\uB85C count\uB97C \uD558\uB098 \uC904\uC5EC\uC900\uB2E4.\n        //    2) list.get(idx-1) \uACFC list.get(idx) \uCC28\uC774\uAC00 1\uBCF4\uB2E4 \uD06C\uBA74 count\uB97C \uD558\uB098 \uC99D\uAC00\uC2DC\uCF1C\uC900\uB2E4.\n        //    3) list.get(idx) \uACFC list.get(idx+1) \uCC28\uC774\uAC00 1\uBCF4\uB2E4 \uD06C\uBA74 count\uB97C \uD558\uB098 \uC99D\uAC00\uC2DC\uCF1C\uC900\uB2E4.\n        //    4) total\uC5D0 count\uB97C \uB354\uD574\uC900\uB2E4.\n        //   \u203B \uCD94\uAC00\uD558\uB824\uB294 \uC6D0\uC18C\uAC00 \uC774\uBBF8 list\uC5D0 \uC874\uC7AC\uD558\uBA74 count\uB9CC total\uC5D0 \uB354\uD574\uC8FC\uACE0 \uB9C8\uCE5C\uB2E4.\n\n\n       int total = 0;\n        List<Integer> list = new ArrayList<Integer>();\n        for(int i = 0; i < nums.length; i++) {\n            \n            int count = 0;\n            list = new ArrayList<Integer>();\n            for(int j = i; j >= 0; j--) {\n                int idx = binarySearchUpper(list, nums[j]);\n                if ( idx < list.size() ) {\n                    if ( list.get(idx) == nums[j] ) {\n                        total += count;\n                        continue;\n                    }\n                } \n                \n                list.add(idx, nums[j]);\n\n                // for(int a = 1; a < list.size(); a++) {\n                //     if ( list.get(a) - list.get(a-1) > 1) total++;\n                // }\n\n                if ( idx-1 >= 0 && list.get(idx-1)+1 < list.get(idx) ) count++;\n                if ( idx+1 < list.size() && list.get(idx)+1 < list.get(idx+1) ) count++;\n                if ( idx-1 >= 0 && idx+1 < list.size() ) count--;\n\n                // System.out.println(list + " " + count);\n\n                total += count;\n\n\n            }\n            \n        }\n        \n        return total;\n    }\n\n    private int binarySearchUpper(List<Integer> list, int target) {\n        int start = 0;\n        int end = list.size();\n        int mid;\n\n        while ( start < end ) {\n            mid = (start+end)/2;\n\n            if ( list.get(mid) >= target) {\n                end = mid;\n            } else{\n                start = mid+1;\n            }\n        }\n\n        return end;\n    }\n}\n```
['Binary Search', 'Ordered Set', 'Java']