[Javascript] Sorting, Binary Search & Prefix Sum
Solution: Sorting, Binary Search & Prefix Sum\n\n1. Sort nums in asc order.\n2. Get the prefix sum from the left and right of nums.\n3. For each query, binary s
**Solution: Sorting, Binary Search & Prefix Sum**\n\n1. Sort nums in asc order.\n2. Get the prefix sum from the left and right of nums.\n3. For each query, binary search for the split point - the first element where `nums[i] >= query`\n  From there, we can calculate the absolute difference of the two segments of nums.\n  Segment one: `query * i - leftSum[i - 1]`\n  Segment two: `rightSum[i] - query * (n - i)`\n\n`n = length of nums`, `m = number of queries`\nTime Complexity: `O(n log(n) + m log(n))`\nSpace Complexity: `O(n + m)`\n```\nvar minOperations = function(nums, queries) {\n  nums.sort((a, b) => a - b);\n  let n = nums.length;\n  let left = [...nums], right = [...nums];\n  for (let i = 1; i < n; i++) left[i] += left[i - 1];\n  for (let i = n - 2; i >= 0; i--) right[i] += right[i + 1];\n  let ans = [];\n  for (let query of queries) {\n    let splitIndex = getSplitIndex(query);\n    let leftDiff = splitIndex > 0 ? query * splitIndex - left[splitIndex - 1] : 0;\n    let rightDiff = splitIndex < n ? right[splitIndex] - query * (n - splitIndex) : 0;\n    ans.push(leftDiff + rightDiff);\n  }\n  return ans;\n  \n  function getSplitIndex(query) {\n    let low = 0, high = n - 1;\n    while (low < high) {\n      let mid = Math.floor((low + high) / 2);\n      if (nums[mid] >= query) high = mid;\n      else low = mid + 1;\n    }\n    return nums[low] >= query ? low : n;\n  }\n};\n```
['JavaScript']