Sort + Prefix Sum + Binary Search
Java\n\npublic List<Long> minOperations(int[] nums, int[] queries) {\n    List<Long> result = new ArrayList<>();\n    Arrays.sort(nums);\n\n    long[] prefixSum
# Java\n```\npublic List<Long> minOperations(int[] nums, int[] queries) {\n    List<Long> result = new ArrayList<>();\n    Arrays.sort(nums);\n\n    long[] prefixSum = new long[nums.length];\n    for (int i = 0; i < nums.length; ++i) {\n      prefixSum[i] = (i > 0 ? prefixSum[i - 1] : 0) + nums[i];\n    }\n\n    for (int query : queries) {\n      int index = firstIndexOfMoreOrEqual(nums, query);\n\n      long leftSum = index == 0 ? 0 : prefixSum[index - 1], leftCount = index;\n      long rightSum = prefixSum[prefixSum.length - 1] - leftSum, rightCount = nums.length - leftCount;\n\n      long leftDifference = query * leftCount - leftSum;\n      long rightDifference = rightSum - query * rightCount;\n\n      result.add(leftDifference + rightDifference);\n    }\n\n    return result;\n  }\n\n  private int firstIndexOfMoreOrEqual(int[] nums, int value) {\n    int left = 0, right = nums.length;\n\n    while (left < right) {\n      int middle = left + (right - left) / 2;\n\n      if (nums[middle] < value)\n        left = middle + 1;\n      else\n        right = middle;\n    }\n\n    return left;\n  }\n```\n\n# C++\n```\n\npublic:\n    vector<long long> minOperations(vector<int> &nums, vector<int> &queries) {\n        vector<long long> result;\n        sort(nums.begin(), nums.end());\n\n        vector<long long> prefixSum(nums.size());\n        for (int i = 0; i < nums.size(); ++i) {\n            prefixSum[i] = (i > 0 ? prefixSum[i - 1] : 0) + nums[i];\n        }\n\n        for (int query: queries) {\n            int index = firstIndexOfMoreOrEqual(nums, query);\n\n            long leftSum = index == 0 ? 0 : prefixSum[index - 1], leftCount = index;\n            long rightSum = prefixSum[prefixSum.size() - 1] - leftSum, rightCount = nums.size() - leftCount;\n\n            long leftDifference = query * leftCount - leftSum;\n            long rightDifference = rightSum - query * rightCount;\n\n            result.push_back(leftDifference + rightDifference);\n        }\n\n        return result;\n    }\n\nprivate:\n    int firstIndexOfMoreOrEqual(vector<int> &nums, int value) {\n        int left = 0, right = nums.size();\n\n        while (left < right) {\n            int middle = left + (right - left) / 2;\n\n            if (nums[middle] < value)\n                left = middle + 1;\n            else\n                right = middle;\n        }\n\n        return left;\n    }\n```
['C++', 'Java']