C++ Solution || Sorting || Lower Bound || Prefix Sum
\ncpp []\nclass Solution {\npublic:\n    vector<long long> minOperations(vector<int>& nums, vector<int>& queries) {\n        // Sort the array\n        sort(num
\n```cpp []\nclass Solution {\npublic:\n    vector<long long> minOperations(vector<int>& nums, vector<int>& queries) {\n        // Sort the array\n        sort(nums.begin(), nums.end());\n        \n        // Precompute the prefix sum\n        int n = nums.size();\n        vector<long long> prefixSum(n + 1, 0); // Use long long for prefixSum\n        for (int i = 1; i <= n; i++) {\n            prefixSum[i] = prefixSum[i - 1] + nums[i - 1];\n        }\n\n        vector<long long> ans(queries.size());\n\n        // For each query, calculate the required number of operations\n        for (int i = 0; i < queries.size(); i++) {\n           int q = queries[i];\n            \n            // Find the position where nums[pos] > q using binary search\n            int pos = lower_bound(nums.begin(), nums.end(), q) - nums.begin();\n            \n            // Left part: nums[0 ... pos-1] are less than or equal to q\n            long long leftSum =(long long) q * pos - prefixSum[pos];\n            \n            // Right part: nums[pos ... n-1] are greater than q\n            long long rightSum = (prefixSum[n] - prefixSum[pos]) - (long long)q * (n - pos);\n            \n            // Total number of operations for this query\n            ans[i] = leftSum + rightSum;\n        }\n\n        return ans;\n    }\n};\n\n```
['C++']