Simple PrefixSum and BinarySearch
IntuitionThe problem involves calculating the minimum number of operations needed to make all elements in the array nums equal to each query value in the querie
# Intuition
The problem involves calculating the minimum number of operations needed to make all elements in the array nums equal to each query value in the queries array. Since operations involve incrementing or decrementing elements by 1, we can simplify the calculation by pre-sorting the array and using a prefix sum to quickly compute the number of operations for each query.

# Approach
1. Sorting: First, sort the nums array. This helps in efficiently finding the elements that are less than or equal to each query using binary search.

1. Prefix Sum: Compute a prefix sum array (arr) where arr[i] holds the sum of the first i elements of nums. This allows quick calculation of sums of subarrays, which is needed to calculate the number of operations.

1. Binary Search: For each query, use binary search to find the largest index where the element in nums is less than or equal to the query value. This index divides the array into two parts:

    - Elements less than or equal to the query.
    - Elements greater than the query.
1. Calculating Operations:

    - Left part: The number of operations to convert the first part of the array to the query value is the difference between the sum of this part and the total sum if all elements were equal to the query.
    - Right part: Similarly, calculate the number of operations for the second part.
    - The total number of operations for each query is the sum of the operations for both parts.
1. Store Results: For each query, store the computed operations in a list and return this list at the end.

# Complexity
- Time complexity:
    - Sorting the array takes O(nlogn).
    - Constructing the prefix sum array takes O(n).
    - For each query, binary search takes O(logn) and calculating the operations takes O(1).
    - If there are m queries, the total time complexity is O(nlogn+mlogn).
- Space complexity:
The space complexity is O(n) for the prefix sum array and O(m) for storing the results, which gives an overall space complexity of O(n+m).

# Code
```java []
class Solution {
    public List<Long> minOperations(int[] nums, int[] queries) {
        Arrays.sort(nums);
        int n = nums.length;
        long[] arr = new long[n + 1];
        for (int i = 0; i < n; i++) {
            arr[i + 1] = arr[i] + nums[i];
        }
        List<Long> list = new ArrayList<>();
        for (int query : queries) {
            int index = binarySearch(nums, query);
            long left = (long) index * query - arr[index];
            long right = (arr[n] - arr[index]) - (long) (n - index) * query;
            list.add(left + right);
        }

        return list;
    }
    private static int binarySearch(int[] nums, int query) {
        int low = 0, high = nums.length;
        while (low < high) {
            int mid = (low + high) / 2;
            if (nums[mid] <= query) {
                low = mid + 1;
            } else {
                high = mid;
            }
        }
        return low;
    }
}
```
['Binary Search', 'Sorting', 'Prefix Sum', 'Java']