[Java] Prefix Sums + Binary Search | easy
Code\njava []\nclass Solution {\n    private int n;\n    public List<Long> minOperations(int[] nums, int[] queries) {\n        List<Long> res = new ArrayList<>(
# Code\n```java []\nclass Solution {\n    private int n;\n    public List<Long> minOperations(int[] nums, int[] queries) {\n        List<Long> res = new ArrayList<>();\n        Arrays.sort(nums);\n        // Init prefixSums\n        this.n = nums.length;\n        long[] prefix = new long[n + 1];\n        for(int i = 1; i <= n; i++)\n            prefix[i] = prefix[i - 1] + nums[i - 1];\n\n        for(int query: queries){\n            // Binary search\n            int pivot = binarySearch(nums, query);\n            long increase = pivot * (1L * query) - prefix[pivot];\n            long decrease = prefix[n] - prefix[pivot] - ((n - pivot) * 1L * query);\n            res.add(increase + decrease);\n        }\n        return res;\n    }\n    int binarySearch(int arr[], int x){\n        int lo = 0, hi = n;\n        while(lo < hi){\n            int m = lo + ((hi - lo) >> 1);\n            if (arr[m] < x)\n                lo = m + 1;\n            else\n                hi = m;\n        }\n        return lo;\n    }\n}\n```
['Array', 'Binary Search', 'Sorting', 'Prefix Sum', 'Java']