Efficient JS solution (Beat 100 time and 75% memory)
\n\n# Complexity\n- let n = nums.length, m = max(nums)\n- Time complexity: O(n(n+m))\n- Space complexity: O(m)\n\n# Code\njs\nconst counters = new Uint8Array(10
![image.png](https://assets.leetcode.com/users/images/4bfe1375-0632-493d-bda0-b66a7be25a29_1696882754.6193466.png)\n\n# Complexity\n- let `n = nums.length`, `m = max(nums)`\n- Time complexity: $$O(n(n+m))$$\n- Space complexity: $$O(m)$$\n\n# Code\n```js\nconst counters = new Uint8Array(1002);\n\n/**\n * @param {number[]} nums\n * @return {number}\n */\nvar sumImbalanceNumbers = function (nums) {\n  let res = 0;\n  let n = nums.length;\n\n  let num;\n  let cur;\n  for (let r = 1; r < n; r++) {\n    counters.fill(0, 0, n+2);\n    num = nums[r];\n    counters[num] = 1;\n    cur = 0;\n\n    for (let l = r-1; l >= 0; l--) {\n      num = nums[l];\n\n      if (!counters[num]) {\n        counters[num] = 1;\n        cur += 1;\n\n        if (counters[num+1]) cur--;\n        if (counters[num-1]) cur--;\n      }\n\n      res += cur;\n    }\n  }\n\n  return res;\n};\n```
['Array', 'JavaScript']