Sum of imbalance Numbers of all Subarrays
\n\n# Code\n\nclass Solution {\npublic:\nint sumImbalanceNumbers(vector<int>& nums) {\n\n    int ans = 0;\n    int n = nums.size();\n    vector<int> G(n + 1,0);
\n\n# Code\n```\nclass Solution {\npublic:\nint sumImbalanceNumbers(vector<int>& nums) {\n\n    int ans = 0;\n    int n = nums.size();\n    vector<int> G(n + 1,0);\n\n    for(int i = 0;i < nums.size();i++){\n        G[nums[i]] = 1;\n        int prev = 0;\n        int maxi = nums[i];\n        int mini = nums[i];\n        // 2,3,1,4\n        for( int j = i+1 ; j < nums.size() ;j++){\n            if(G[nums[j]] == 0){\n                if(G[nums[j] - 1] == 1 && maxi > nums[j]-1){\n                    prev--;\n                }\n                if(G[nums[j] - 1] == 0 && mini < nums[j]-1 && (maxi==mini || nums[j] > maxi)){\n                    prev++;\n                }\n                if(nums[j] + 1 <= n && G[nums[j]+1] == 0 && maxi > nums[j]+1){\n                    prev++;\n                }\n                G[nums[j]] = 1;\n            }\n            ans = ans + prev;\n            maxi = max(maxi , nums[j]);\n            mini = min(mini , nums[j]);\n        }\n        for(int i = 0;i <= n;i++){\n            G[i] = 0;\n        }\n    }        \n    return ans;\n}\n\n};\n```
['C++']