Simple binary search
\njava []\nclass Solution {\n    public List<Long> minOperations(int[] nums, int[] queries) {\n       List<Long>ans=new ArrayList<>();\n       Arrays.sort(nums)
\n```java []\nclass Solution {\n    public List<Long> minOperations(int[] nums, int[] queries) {\n       List<Long>ans=new ArrayList<>();\n       Arrays.sort(nums);\n       long[]pre=new long[nums.length];\n       pre[0]=nums[0];\n       for(int i=1;i<pre.length;i++){\n        pre[i]=pre[i-1]+nums[i];\n       } \n       for(int i=0;i<queries.length;i++){\n        int x=queries[i];\n        int l=f1(x,nums);\n        int r=f2(x,nums);\n      // System.out.println(l+" "+r);\n       \n        long aa=(long)x*(l+1)-(l==-1?0:pre[l])+pre[nums.length-1]-(r==0?0:pre[r-1])-(long)x*(nums.length-r);\n        ans.add(aa);\n       }\n       return ans;\n    }\n    public int f1(int t,int[]a){\n        int l=0;\n        int r=a.length-1;\n        int ans=-1;\n        while(l<=r){\n         int m=(l+r)/2;\n         if(a[m]<t){\n            ans=m;\n            l=m+1;\n         }\n         else{\n           r=m-1;\n         }\n        }\n        return ans;\n    }\n     public int f2(int t,int[]a){\n        int l=0;\n        int r=a.length-1;\n         int ans=a.length;\n        while(l<=r){\n         int m=(l+r)/2;\n         if(a[m]>t){\n            ans=m;\n            r=m-1;\n         }\n         else{\n           l=m+1;\n         }\n        }\n        return ans;\n    }\n}\n```
['Java']