beat 100% memory
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution(object):\n    def minOperations(self, nums, queries):\n        nums.sort()  # Sort the numbers once\n        numsLen = len(nums)\n        prefix_sums = [0] * (numsLen + 1)\n\n        # Compute prefix sums for absolute differences\n        for i in range(1, numsLen + 1):\n            prefix_sums[i] = prefix_sums[i - 1] + nums[i - 1]\n\n        output = []\n        for query in queries:\n            if query >= nums[-1]:\n                # Calculate directly since all nums are less than or equal to query\n                output.append(sum(query - num for num in nums))\n            else:\n                # Use binary search to find the split point\n                l, r = 0, numsLen - 1\n                while l <= r:\n                    mid = (l + r) // 2\n                    if nums[mid] > query:\n                        r = mid - 1\n                    else:\n                        l = mid + 1\n\n                # l is now the count of numbers <= query\n                sum_lower = l * query - prefix_sums[l]  # Sum for numbers <= query\n                sum_higher = (prefix_sums[numsLen] - prefix_sums[l]) - (numsLen - l) * query  # Sum for numbers > query\n                output.append(sum_lower + sum_higher)\n\n        return output\n\n```
['Python']