Presum + BinarySearch
IntuitionUsing Presum and Binary searchApproachComplexity
Time complexity: NlogN + MlogN (N is length of nums, M is length of queries)

Space complexity:
O(N)
C
# Intuition
Using Presum and Binary search


# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity: NlogN + MlogN (N is length of nums, M is length of queries)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
O(N)

# Code
```java []
class Solution {
    public List<Long> minOperations(int[] nums, int[] queries) {
        List<Long>res = new ArrayList<>();
        Arrays.sort(nums);
        long[] preSum = new long[nums.length];
        long sum = 0l;
        int len = nums.length;
        for(int i=0;i<nums.length;i++){
            sum += nums[i];
            preSum[i] = sum;
        }

        for(int query: queries){
            int pos = getPos(nums, query);
            if(pos == -1 || pos == 0){
                long count = preSum[nums.length-1] - len * (long)query;
                res.add(count);
            }else if(pos == nums.length){
                long count = len * (long)query - preSum[nums.length-1];
                res.add(count);
            }else{
                long count = ((long)query * pos - preSum[pos-1]) + ((preSum[nums.length-1] - preSum[pos-1]) - (long)query * (len - pos));
                res.add(count);
            }
        }
        
        return res;
        
    }
    private int getPos(int[] nums, int target){
        int left = 0, right = nums.length-1;
        while(left <= right){
            int mid = left  + (right - left)/2;
            if(nums[mid] <= target){
                left = mid+1;
            }else{
                right = mid-1;
            }
        }

        return left;
    }
}
```
['Java']