💯🔥Best Code in C++ || BinarySearch🔥 || Sorting✅ || PrefixSum💯
Complexity\n- Time complexity:\nO(mlogn)\n\n- Space complexity:\nO(n)\n\n# Code\n### Please Upvote if u liked my Solution\uD83E\uDD17\n\nclass Solution {\npubli
# Complexity\n- Time complexity:\nO(mlogn)\n\n- Space complexity:\nO(n)\n\n# Code\n### Please Upvote if u liked my Solution\uD83E\uDD17\n```\nclass Solution {\npublic:\n    vector<long long> minOperations(vector<int>& nums, vector<int>& queries) {\n        int n=nums.size(),m=queries.size();\n        long long pSum=0;\n        sort(nums.begin(),nums.end());\n        vector<long long> prefixSum(n),ans;\n        for(int i=0;i<n;i++){\n            pSum+=nums[i];\n            prefixSum[i]=pSum;\n        }\n        for(auto it:queries){\n            auto ub=upper_bound(nums.begin(),nums.end(),it);\n            int ind=ub-nums.begin();\n            long long minOper=0, req_sum=0, curr_sum;\n            if(ind>0){\n                req_sum = (long)it * ind, curr_sum = prefixSum[ind-1];\n                minOper = req_sum - curr_sum;\n            }\n            req_sum = (long)it * (n-ind), curr_sum = (ind>0)? (prefixSum[n-1]-prefixSum[ind-1]) : prefixSum[n-1];\n            minOper += curr_sum - req_sum;\n            ans.push_back(minOper);\n        }\n        return ans;\n    }\n};\n```\n![e2515d84-99cf-4499-80fb-fe458e1bbae2_1678932606.8004954.png](https://assets.leetcode.com/users/images/e7ad9666-2c12-4662-98d1-859bc5d261e5_1679819969.3035681.png)\n
['Binary Search', 'Sorting', 'Prefix Sum', 'C++']