[Java/Python 3] Sort, compute prefix sum then binary search.
Within each query, for each number in nums, if it is less than q,  we need to deduct it from q and get the number of  the increment operations; if it is NO less
Within each query, for each number in `nums`, if it is less than `q`,  we need to deduct it from `q` and get the number of  the increment operations; if it is NO less than `q`, we need to deduct `q` from the number to get the number of the decrement operations.\n\nTherfore, we can sort `nums`, then use prefix sum to compute the partial sum of the sorted `nums`. Use binary search to find the index of the `nums`, `idx`, to separate the less than  part and NO less than part. With these information we can compute the total operations needed for a specific query `q`.\n\n**Note:**\n`nums[0 ...  idx - 1] <= q, nums[idx ... n - 1] > q`\n`prefixSum[idx] = sum(nums[0 ...  idx - 1])`\n`prefixSum[n] - prefixSum[idx] = sum(nums[idx ... n - 1])`\n`idx * q - prefixSum[idx]` : # of increment operations needed for `nums[0 ...  idx - 1]`\n`prefixSum[n] - prefixSum[idx] - (n - idx) * q`:  # of  decrement operations needed for `nums[idx ... n - 1]`\n```java\n    public List<Long> minOperations(int[] nums, int[] queries) {\n        Arrays.sort(nums);\n        int n = nums.length;\n        long[] prefixSum = new long[n + 1];\n        for (int i = 0; i < n; ++i) {\n            prefixSum[i + 1] = prefixSum[i] + nums[i];\n        }\n        List<Long> ans = new ArrayList<>();\n        for (int q : queries) {\n            int idx = binarySearch(nums, q); // There are idx numbers in nums less than q.\n            ans.add(1L * q * idx - prefixSum[idx] + prefixSum[n] - prefixSum[idx] - 1L * (n - idx) * q);\n        }\n        return ans;\n    }\n    private int binarySearch(int[] nums, int key) {\n        int lo = 0, hi = nums.length;\n        while (lo < hi) {\n            int mid = lo + (hi - lo) / 2;\n            if (key > nums[mid]) {\n                lo = mid + 1;\n            }else {\n                hi = mid;\n            }\n        }\n        return lo;\n    }\n```\n```python\n    def minOperations(self, nums: List[int], queries: List[int]) -> List[int]:\n        nums.sort()\n        prefix_sum, n = [0], len(nums)\n        for num in nums:\n            prefix_sum.append(num + prefix_sum[-1])\n        ans = []\n        for q in queries:\n            idx = bisect.bisect(nums, q) # There are idx numbers in nums less than q.\n            ans.append(q * idx - prefix_sum[idx] + prefix_sum[-1] - prefix_sum[idx] - q * (n - idx))    \n        return ans\n```\n\n**Analysis:**\n\nLet `n = nums.length, m = queries.length`:\nSort cost time `O(nlogn)`, each query cost time `O(logn)` and all quries cost `O(mlogn)`; `prefixSum` and sorting cost space `O(n)`, therefore\n\nTime: `O((m + n) * logn)`, space: `O(n)` (`O(m + n)` if including return space).
['Java', 'Python3']