Easy SWT JAVA SOLUTION || 1ms 100 % Working 🔥🔥🔥
\n# Here\'s a shorter explanation of the solution:\n\n1. Initialize arrays left, right, and last, all of size n+2, with default values.\n2. Iterate over the inp
\n# Here\'s a shorter explanation of the solution:\n\n1. Initialize arrays `left`, `right`, and `last`, all of size `n+2`, with default values.\n2. Iterate over the input list `A`:\n   - Update `left[i]` with the previous occurrence of `A[i] + 1`.\n   - Update `right[i]` with the minimum of the next occurrences of `A[i]` and `A[i] + 1`.\n   - Calculate and accumulate `(i - left[i]) * (right[i] - i)` to `ans`.\n   - Update the last occurrence of `A[i]` to the current index `i`.\n3. Return `ans - n * (n + 1) / 2` as the sum of imbalanced numbers, excluding balanced numbers.\n\n# Code\n```\nclass Solution {\n    public int sumImbalanceNumbers(int[] A) {\n        int n = A.length;\n        int[] left = new int[n];\n        Arrays.fill(left, -1);\n        int[] last = new int[n + 2];\n        Arrays.fill(last, -1);\n        for (int i = 0; i < n; i++) {\n            left[i] = last[A[i] + 1];\n            last[A[i]] = i;\n        }\n        int[] right = new int[n];\n        Arrays.fill(right, n);\n        Arrays.fill(last, n);\n        for (int i = n - 1; i >= 0; i--) {\n            right[i] = Math.min(last[A[i]], last[A[i] + 1]);\n            last[A[i]] = i;\n        }\n        int ans = 0;\n        for (int i = 0; i < n; i++) {\n            ans += (i - left[i]) * (right[i] - i);\n        }\n        return ans - n * (n + 1) / 2;\n    }\n}\n\n```
['Array', 'Hash Table', 'Ordered Set', 'Java']