Detailed solution, Easy to Understand || C++
Intuition\nAt the best case, for a subarray of size s we can have a maximum of s-1 imbalance, say {1,3,5,7,9} the imbalance is 4, which is, size-1\nNow let\'s s
# Intuition\nAt the best case, for a subarray of size `s` we can have a maximum of `s-1` imbalance, say `{1,3,5,7,9}` the imbalance is `4, which is, size-1`\nNow let\'s say we include a new element `10` so our subarrray is `{1,3,5,7,9,10}`. Now 10 is just 1 greater than 9, so it will not increase our imbalance. So our new imbalance will be `size-1-1(imbalance due to 10)`, which is, we can substract the number of balances occuring due to addition of new element. More on this in approach section\n\n# Approach\n- Starting with each index from `i=0 to i=n-2`\n- We will declare a `vis` array (Can use map also for optimisation)\n- Mark `vis[nums[i]]` as true\n    - Now for every element form `j=i+1 to j=n-1` \n    - We will add each element in our vis array\n    - While adding the elements we will check for the new element if it is killing the oppourtinity to create a new imbalance \n    - Say new element is `t` so if we have any element `t-1` or `t+1` present in the vis array, this will kill the oppourtinity for the new element to create a imbalance\n    \n    ```Example []\n    Say the complete array is {1,3,5,7,8,4}\n\n        For i=0\n            vis[0]=1                      vis is true for {1}\n            Imbalance is size-1=0\n\n        For j=1\n        We will add 3                 vis is true for {1,3}\n            Now we don\'t have 2 or 4 in our current subarray i-j\n            So 3 will not kill any imbalance, \n            so imbalance += size-1, which is 1\n            \n        For j=2\n        We will add 5                 vis is true for {1,3,5}\n            Now we don\'t have 4 or 6 in our current subarray i-j\n            So 5 will not kill any imbalance, \n            so imbalance += size-1, which is 2\n\n        For j=3\n        We will add 7                 vis is true for {1,3,5,7}\n            Now we don\'t have 6 or 8 in our current subarray i-j\n            So 7 will not kill any imbalance, \n            so imbalance += size-1, which is 3\n\n        For j=4\n        We will add 8                 vis is true for {1,3,5,7,8}\n            We have 7 already present, so there is no imbalance between\n            7 & 8, so will increment our kill by one,\n            so imbalance += size-1-kill, which is 3\n\n        For j=5\n        We will add 4                 vis is true for {1,3,4,5,7,8}\n            Now we have both 3 and 5 in our visited arrar, so will\n            increment kill 2 times and now kill will be `3`\n            So the imbalance for subarray i-j will be \n                size-1-kill, which is 2\n\n\n    ```\n    - We will keep adding to our answer `size-1-kill`, `j-i-kill` to our answer for each combinition of `i & j`\n\n    - Also, if we encounter duplicate elements, those elements too won\'t contribute to imbalance, so we will increment our kill.\n    We need not to check again for the `duplicate elements`\'s immidiate neighbours as in sorted array, the position of them will be with there 1st occurance\n\n    - Say {1,3,5,7} has imbalance 3, which is size-1\n        - If 5 comes once more, {1,3,5,5,7} the imbalance will be `size-1-count(duplicate)` so 3\n\n# Complexity\n- Time complexity:\n$$O(N^2)$$\n\n- Space complexity:\n$$O(N)$$\n\n# Code\n``` cpp []\nclass Solution {\npublic:\n    int solve(vector<bool>& vis,int i){\n        int ans = 0;\n        vis[i]=1;\n        if(i-1>=0 && vis[i-1]!=0) ans++;\n        if(i+1<vis.size() && vis[i+1]!=0) ans++;\n        return ans;\n    }\n    int sumImbalanceNumbers(vector<int>& nums) {\n        int ans=0,n=nums.size();\n        for(int i=0;i<n-1;i++){\n            vector<bool> vis(1001,0);\n            int kill=0;\n            vis[nums[i]]=1;\n            for(int j=i+1;j<n;j++){\n                if(vis[nums[j]]==1) kill++;\n                else kill += solve(vis,nums[j]);\n                ans += max(0,(j-i-kill));\n            }\n            cout<<i<<" "<<ans<<endl;\n        }\n        return ans;\n    }\n};\n```\n\n\n---\n# Please do upvote if you find my solution helpful \uD83D\uDE0A
['Array', 'Hash Table', 'Math', 'Two Pointers', 'C++']