c++ | 20 lines 2 for loop
Intuition\n Describe your first thoughts on how to solve this problem. \nwhetever given in the question is you have to do . but you have to chosse right data st
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nwhetever given in the question is you have to do . but you have to chosse right data structure . i.e ordered_set and multiset  \n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n    O(N * N * log N )\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n    O(N) - set maximum size \n# Code\n```\nclass Solution {\npublic:\n    int sumImbalanceNumbers(vector<int>& nums) {\n        int n = nums.size() ;\n        int ans = 0 ; \n        for(int i = 0 ; i< n ; ++i ){\n            multiset<int> st ; \n            int prev = 0 ; \n            for( int j = i ; j < n ; ++j ){\n                auto it = st.lower_bound(nums[j]) ;\n                if(j == i ) {} \n                else if(it == st.end() ){\n                    --it ; \n                    if(nums[j] - *it > 1 )  ++prev ; \n                }\n                else if(it == st.begin() ){\n                    if( *it - nums[j] > 1 )  ++prev ; \n                }\n                else{\n                    int right = *it ; \n                    --it ; \n                    int left = *it ; \n                    if(right - left > 1 ) --prev ; \n                    if(right - nums[j] > 1 ) ++prev ; \n                    if( nums[j] - left > 1 ) ++prev ;\n                }\n                st.insert( nums[j] ) ;\n                ans += prev ; \n            }\n            \n            // cout<<ans<<" ";\n        }\n        return ans ; \n    } \n};\n```
['Ordered Set', 'C++']