Easy solution using Binary  Search
Code\njava []\nclass Solution {\n    public List<Long> minOperations(int[] nums, int[] q) {\n        Arrays.sort(nums);\n        List<Long> ans = new ArrayList<
# Code\n```java []\nclass Solution {\n    public List<Long> minOperations(int[] nums, int[] q) {\n        Arrays.sort(nums);\n        List<Long> ans = new ArrayList<>();\n        int n = nums.length;\n        int m = q.length;\n        long[] pre = new long[n];\n        pre[0] = nums[0];\n        for(int i=1;i<n;i++){\n            pre[i] = pre[i-1]+nums[i];\n        }\n\n        for(int i=0;i<m;i++){\n            int query = q[i];\n\n            int idx = helper(nums, query);\n            if(idx==n){\n                ans.add((n*1L*query)-pre[n-1]);\n            }else{\n                long right = (pre[n-1]-pre[idx]+nums[idx])-(n-idx)*1L*query;\n                long left = 0;\n                if(idx!=0){\n                    left = (idx*1L*query)-(pre[idx-1]-pre[0]+nums[0]);\n                }\n                ans.add(left + right);\n            }\n        }\n        return ans;\n    }\n\n    public int helper(int[] nums, int target){\n        int low = 0, high = nums.length-1;\n\n        while(low<=high){\n            int mid = low+(high-low)/2;\n\n            if(nums[mid]<=target){\n                low = mid+1;\n            }else{\n                high = mid-1;\n            }\n        }\n        return low;\n    }\n}\n```
['Array', 'Binary Search', 'Sorting', 'Prefix Sum', 'Java']