C++ | Prefix Sum | Binary Search | Sorting | Math
Code\n\nclass Solution {\npublic:\n    vector<long long> minOperations(vector<int>& nums, vector<int>& queries) {\n        sort(nums.begin(),nums.end());\n     
# Code\n```\nclass Solution {\npublic:\n    vector<long long> minOperations(vector<int>& nums, vector<int>& queries) {\n        sort(nums.begin(),nums.end());\n        int n=nums.size();\n        vector<long long> pref(n,0);\n        pref[0]=nums[0];\n        for(int i=1;i<n;i++)\n            pref[i]=nums[i]+pref[i-1];\n        vector<long long> ans;\n        for(long long q:queries){\n            int ind=upper_bound(nums.begin(),nums.end(),q)-nums.begin();\n            long long diff=0;\n            if(ind-1>=0){\n                diff+=q*ind-pref[ind-1];\n                diff+=pref[n-1]-pref[ind-1]-(n-ind)*q;\n            }\n            else\n                diff+=pref[n-1]-n*q;\n            ans.push_back(diff);\n        }\n        return ans;\n    }\n};\n```
['Math', 'Binary Search', 'Prefix Sum', 'C++']