C++ | 95% Fast | With best explanation
Intuition\nDivide nums into 2 subarray\nsuppose we have nums = [2,6,8] and we need to find operations for query [5]. \nJust find elements that are greater than 
# Intuition\nDivide nums into 2 subarray\nsuppose we have nums = [2,6,8] and we need to find operations for query [5]. \nJust find elements that are greater than 5 and that are lower than it. Here, [6,8] are greater and [2] is lower.\n\nFor greater, sum of these elements is 6+8 = 14. We need to bring these 2 elements equal to 5. Then there sum would be 10. That means we need to perform 14-10 = 4 operations.\n\nFor lower element, current sum is 2. We need to make it 5. That means we need (5 - 2) = 3 operations.\n\n# Approach\nUse prefix sum to fetch sum in O(1)\nFind lenght of subarrays using binary search(upper_bound)\n\n# Code\n```\nclass Solution {\npublic:\n    vector<long long> minOperations(vector<int>& nums, vector<int>& queries) {\n        sort(nums.begin(), nums.end());\n        vector<long long> sum;\n        \n        // Prefix Sum\n        long long sumi = 0;\n        for(auto &i : nums){\n            sumi += i;\n            sum.push_back(sumi);\n        }\n\n        vector<long long> ans;\n\n        for(auto &i : queries){\n            long long cnt = 0;\n\n            // Length of bigger elements subarray\n            long long bigger = upper_bound(nums.begin(), nums.end(), i) - nums.begin();\n            bigger = nums.size() - bigger;\n\n            // Lenght of smaller elements subarray\n            long long shorter = nums.size() - bigger - 1;\n            while(shorter >= 0 && nums[shorter] == i) shorter--;\n            shorter = shorter + 1;\n\n            // Sum of greater and smaller elements\n            long long leftSum = 0, rightSum = 0;\n            if(shorter > 0) leftSum = sum[shorter-1];\n            if(bigger > 0){\n                rightSum = sum.back();\n                if(bigger != nums.size()) rightSum -= sum[nums.size() - bigger - 1];\n            }\n\n            cnt += rightSum - (bigger * i);\n            cnt += (shorter * i) - leftSum;\n\n            ans.push_back(cnt);\n        }\n\n        return ans;\n    }\n};\n```
['C++']