O(mlogn) solution C++|| beats 85%
Intuition\n Describe your first thoughts on how to solve this problem. \nintuition was to find a pattern and generalize it.\n\n# Approach\n Describe your approa
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nintuition was to find a pattern and generalize it.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nI find a number just greater than the query element and calculate the left side contribution, that is difference we need to increase the numbers in left by, to make it equal to query[i], similarly for right elements and add them up to get res[i].\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n    O(mlog(n))\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n    O(n+m)\n\n# Code\n```cpp []\nclass Solution {\n    #define ll long long\npublic:\n    vector<long long> minOperations(vector<int>& nums, vector<int>& queries) {\n        int n = nums.size();\n        int m = queries.size();\n        vector<ll> res(m);\n\n        sort(nums.begin(), nums.end());\n        vector<ll> prefix(n);\n        prefix[0] = nums[0];\n        for (int i = 1; i < n; i++) {\n            prefix[i] = prefix[i - 1] + nums[i];\n        }\n\n        for (int i = 0; i < m; i++) {\n            int query = queries[i];\n            int pivot = getJustBiggerIndex(nums, query);\n            \n            // Left side contribution (elements <= query)\n            ll leftSum = (pivot > 0) ? prefix[pivot - 1] : 0;\n            ll leftCount = pivot;\n            ll leftOperations = query * leftCount - leftSum;\n\n            // Right side contribution (elements > query)\n            ll rightSum = (pivot < n) ? prefix[n - 1] - leftSum : 0;\n            ll rightCount = n - leftCount;\n            ll rightOperations = rightSum - query * rightCount;\n\n            res[i] = leftOperations + rightOperations;\n        }\n\n        return res;\n    }\n\n    int getJustBiggerIndex(vector<int>& nums, int query) {\n        int i = 0, j = nums.size() - 1;\n        while (i <= j) {\n            int mid = i + (j - i) / 2;\n            if (nums[mid] <= query) {\n                i = mid + 1;\n            } else {\n                j = mid - 1;\n            }\n        }\n        return i;\n    }\n};\n\n```
['C++']