JavaScript Big Brain Solution. +500 IQ😤
Intuition\n Describe your first thoughts on how to solve this problem. \nGiven a 3 number group a, b, c where a < b < c it can be proved that the group (a c) wo
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nGiven a 3 number group `a, b, c` where `a < b < c` it can be proved that the group `(a c)` would have an imbalance of 1 (Because c is at least +2 bigger than a)\nKeeping `b` between `a` and `c` could either remove the imbalance e.g `1 2 3`, do nothing to the imbalance e.g `1 4 5` or add 1 to the imbalance e.g `1 3 5`\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nThe first thing I did was eliminate the edge case\n### Edge Case 1: `a` `b` and `c` are not unique\nIn this case I just skipped it since `1 3` and `1 3 3` or `1 1 3` have the same imbalance\n\n### Edge Case 2: `a` or `c` does not exist\nIf the new number `b` appears at the end or beginning of the sorted array then it won\'t have anything after or before it. In those cases I checked if adding it to behind the max or in front of the min would cause an imbalance and added it\n\nAs an example if 4 is the least and 2 was added to the array the imbalance will be increase by 1 and the sorted array will look something like this `2, 4, ...`\n\n---\nConsidering we have made sure `a` `b` and `c` are different we can check to see if the number `b-1` exists to see if we have an imbalance in group `a b` and `c-1` for `b c`\n\n---\n\nWith the edge cases out we can get into the good part\n\nWe have already established group `a c` where `a > b > c` has an imbalance of 1\n\nWe could create new groups `a b` and `b c` to replace group `a c` where `a b` replaces `a c` and `b c` is a new group\n\nSo if `a b` does not have an imbalance we subtract one since `a c` had one and if `a b` has an imbalance we do nothing for the same reason\n\nLikewise if `b c` has an imbalance we add 1 since `b c` is a new group that didn\'t exist before but if `b c` has no imbalance we do nothing for the same reason as stated before\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(n*n)$$\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$$O(n)$$\n\n# Code\n```javascript\n/**\n * @param {number[]} nums\n * @return {number}\n */\n\nvar sumImbalanceNumbers = function(nums) {\n  let ans = 0\n  for(let i = 0; i < nums.length-1; i++){\n    const set = new Set([nums[i]])\n    let max = nums[i], min = nums[i], imbalance = 0\n    for(let j = i+1; j < nums.length; j++){\n      if(!set.has(nums[j])){\n        if(max < nums[j]){\n          if(nums[j] > max+1)imbalance++\n          max = nums[j]\n        }else if(min > nums[j]){\n          if(nums[j] < min-1)imbalance++\n          min = nums[j]\n        }else{\n          if(set.has(nums[j]-1))imbalance--\n          if(!set.has(nums[j]+1))imbalance++\n        }\n        set.add(nums[j])\n      }\n      ans += imbalance\n    }\n  }\n  return ans\n};\n```
['JavaScript']