[Entire thought Process + Intuition, Binary Search + Prefix Sum Approach]
Intuition\n\n    * Let\'s try, to to make some observations:\n\n    Input: nums = [3,1,6,8], queries = [1,5]\n    Output: [14, 10]\n    \n    queires[0] = 1, an
# Intuition\n\n    * Let\'s try, to to make some observations:\n\n    Input: nums = [3,1,6,8], queries = [1,5]\n    Output: [14, 10]\n    \n    queires[0] = 1, ans = 14, \n    answer 14 can be easily compute as (total_sum_of_nums - nums_size * queries[0]) -> (18 - 4 * 1) -> 14\n\n    queries[1] = 5, ans = 10, \n    but the above point is NOT always valid for all "i"th queries.\n    \n    For example here, (18 - 4 * 5) -> -2 \n    \n    #1 -ve is indicating that we\'ve to add some more items on couple of indices.\n    \n    #2 -ve answer also indicating that some of the elements in "nums" are lesser than "queries[i]"\n    \n    #3 Also total_sum of array(nums) doesn\'t give us the information about the elements that\n        are at different indices in "nums" array.\n        \n    #4 From [3, 1, 6, 8], queries[1] = 5, this thought is came from Obs#2\n    \n            Q: all the elements that are smaller(or equal can be added here)\n                    than (5 : current ith query) would contribute how much in our final answer?\n                    \n            A: (count_of_elements * queries[i] - sum_of_all_those_elements)\n            \n                for example, here for the 1st query:\n                \n                cont_of_elements = 2 -> {1, 3} -> [1 & 3 are smaller than 5]\n                \n                contribution = (2 * 5 - (1 + 3)) = 6\n                \n                \n                Similarly, total contribution of elements in our final answer from\n                elements larger than queries[i] can be given as:\n                \n                contribution = (sum_of_all_such_elements - count_of_such_elements * queries[i])\n                \n                contribution = ((6 + 8) - (2 * 5)) -> (14 - 10) -> 4\n                \n                \n                now add the contributions = (6 + 4) -> 10 [final answer for 1st query]\n                \n                \n            * Counting part can be efficiently computed using "Binary Search" and for getting a sum of elements\n                for any sub-array can be easily computed in O(1) with the help of "prefix-sum" array.\n                    - But "Binary Search" can be applied only in monotonic data, hence sorting required.\n                \n        \n        +++ Now, I got the approach. Excellent man :).\n\n# Complexity\n- Time complexity: O(N + N * log(N)) -> O(N * log(N))\n- Space complexity: O(N)\n\n# Code\n```\n\nclass Solution {\n    public List<Long> minOperations(int[] nums, int[] queries) {\n        Arrays.sort(nums);\n        \n        int N = nums.length;\n        long[] prefixSum = new long[N];\n        \n        for(int i = 0; i < N; i += 1) {\n            if(i == 0) prefixSum[i] = nums[i];\n            else {\n                prefixSum[i] = prefixSum[i - 1] + nums[i];\n            }\n        }\n                \n        List<Long> answer = new ArrayList<>();\n        \n        for(int i = 0; i < queries.length; i += 1) {\n            int queryEle = queries[i];\n            int idx = upperBound(nums, queryEle);\n            if(idx == N) {\n                answer.add(((N * 1L * queryEle) - prefixSum[N - 1]));\n            } else {\n                long contri1 = (prefixSum[N - 1] - prefixSum[idx] + nums[idx]) - (N - idx) * 1L * queryEle; // for larger > queries[i]\n                long contri2 = 0;\n                if(idx != 0) \n                    contri2 = (idx * 1L * queryEle) - (prefixSum[idx - 1] - prefixSum[0] + nums[0]); // for smaller <= queries[i]\n                answer.add((contri1 + contri2));\n            }\n        }\n        \n        return answer;\n    }\n    \n    // returns the first index (idx) such that nums[idx] > target\n    private int upperBound(int[] nums, int target) {\n        int left = 0, right = nums.length - 1;\n        while(left <= right) {\n            int mid = left + (right - left) / 2;\n            if(nums[mid] <= target)\n                left = mid + 1;\n            else\n                right = mid - 1;\n        }\n        return left;\n    }\n}\n```
['Binary Search', 'Prefix Sum', 'Java']