C++ | New easiest approach | sort + check in O(n) time | beats 100% memory and time complexity❤️‍🔥
Intuition\n Describe your first thoughts on how to solve this problem. \n\nfirst of all , by seeing the constraints we can think the naive approach to solve it 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\nfirst of all , by seeing the constraints we can think the naive approach to solve it by taking two nested loops and moving forward and take any subarray and iterate it for the condition \nsarr[i+1] - sarr[i] > 1\nand iterating count\n\n--> but tle comes into picture and we have to erase sort function and make the array ascending by insertiing the position of the new member at a very particular position and\n\nnew way of checking the whole array to just check consecutive prev array positions and new positions and increse and decrese the imbalance\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\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 count =0;\n        vector<int> arr;\n        for(int i=0 ; i<nums.size()-1;i++)\n        {\n            arr.clear();\n            arr.push_back(nums[i]);\n            int imbalance = 0;\n            for(int j=i+1 ; j<nums.size() ; j++)\n            {\n                int p = nums[j];\n                int index = -1;\n                auto it = lower_bound(arr.begin(), arr.end(), p);   // logn time\n               \n                index = it - arr.begin(); \n                arr.insert(it,p);\n\n                if( (index+1 < arr.size()) && (index-1 >= 0) && (arr[index+1] - arr[index-1] > 1))\n                    imbalance--;\n                if( (index+1 < arr.size()) && (arr[index+1] - arr[index] > 1))\n                    imbalance++;\n                if( (index-1 >= 0) && (arr[index] - arr[index-1] > 1))\n                    imbalance++;\n\n                count += imbalance;\n            }\n            \n        }\n        return count;\n    }\n};\n```
['C++']