Easy Solution in C++ | beats 100 %
Intuition\n Describe your first thoughts on how to solve this problem. \nInstead of finding All the subarray and then counting imbalance elements in each subarr
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nInstead of finding All the subarray and then counting imbalance elements in each subarray.\n\nWe select an element and then count the possible subarrays for that element to be an imbalance element.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nTraverse ith number\nFind the next large number that can contribute to make it imbalance number.\n            Find that contributing number in the left and right of the ith number. Please check [https://youtu.be/n6yWS5TouMw](this video) for more insight.\n\n\n# Complexity\n- Time complexity: O(n^2)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    int sumImbalanceNumbers(vector<int>& nums) {\n        int n=nums.size();\n        int L,R,i,r_contribute,l_contribute;\n        int ans=0;\n        \n        for(i=0;i<n;i++){\n            L=i-1;\n            R=i+1;\n            l_contribute=-1;\n            while(L>-1&&nums[L]!=nums[i]+1){\n                if(l_contribute==-1 && nums[L]-nums[i]>1){\n                    l_contribute=L;\n                }\n                L--;\n            }\n            if(l_contribute==-1)l_contribute=L;\n            \n            r_contribute=-1;\n            while(R<n&&(nums[R]!=nums[i]+1 && nums[R]!=nums[i]) ){\n                if(r_contribute==-1&& nums[R]-nums[i]>1){\n                    r_contribute=R;\n                }\n                R++;\n            }\n            if(r_contribute==-1)r_contribute=R;\n            \n            int right_cnt = R-i;\n            int my_left_cnt = l_contribute-L;\n            ans+=my_left_cnt * right_cnt;\n\n\n            int rem_left_cnt = i-l_contribute;\n            int my_right_cnt = R-r_contribute;\n\n            ans+=rem_left_cnt * my_right_cnt;\n            \n        }\n        return ans;\n    }\n};\n```
['Array', 'C++']