Java -  Easy to understand
Define a array with length equal to $nums$.\n\nTwo loops, the first loop iterate $nums$ represents the head of the subarray, the second loop iterate $nums$ repr
Define a array with length equal to $nums$.\n\nTwo loops, the first loop iterate $nums$ represents the head of the subarray, the second loop iterate $nums$ represents the end of the subarray.\n\nFor each element from iterating, set `array[num] = num`, check has its neighbours already been inserted.\n\n1. There is no neighbours, let `imbalance++`\n2. There is one neighbour, no need to update `imbalance`\n3. There are two neighbours, let `imbalance--`\n```\nclass Solution {\n    public int sumImbalanceNumbers(int[] nums) {\n        int n = nums.length;\n        int size = n + 1;\n        int res = 0;\n        for (int i = 0; i < n - 1; i++) {\n            int[] sArr = new int[size];\n            sArr[nums[i]] = nums[i];\n            int imbalance = 0;\n            for (int j = i + 1; j < n; j++) {\n                int num = nums[j];\n                if (sArr[num] == 0) {\n                    if (num == 1) {\n                        imbalance += sArr[2] == 2 ? 0 : 1;\n                    } else if (num == n) {\n                        imbalance += sArr[n - 1] == n - 1 ? 0 : 1;\n                    } else {\n                        if ((sArr[num - 1] == num - 1) && (sArr[num + 1] == num + 1)) {\n                            imbalance--;\n                        }\n                        if ((sArr[num - 1] != num - 1) && (sArr[num + 1] != num + 1)) {\n                            imbalance++;\n                        }\n                    }\n                    sArr[num] = num;\n                }\n                res += imbalance;\n            }\n        }\n        return res;\n    }\n}\n```
['Java']