C++ Easy Solution | Binary Search + Sorting | O(NlongN)
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    vector<long long> res;\n    \n    static int next(vector<int>& arr, int target) {\n        int start = 0, end = arr.size() - 1;\n        if (end == 0) return -1;\n        if (target > arr[end]) return end;\n\n        int ans = -1;\n        while (start <= end) {\n            int mid = start + (end - start) / 2; // avoid overflow\n            if (arr[mid] >= target) {\n                end = mid - 1;\n            } else {\n                ans = mid;\n                start = mid + 1;\n            }\n        }\n        return ans;\n    }\n   \n    void helper(vector<int>& nums, vector<long long>& prefix, int k) {\n        int n = nums.size();\n        int left = 0;\n        int right = nums.size() - 1;\n        int pos = next(nums, k);\n        \n        long long currRes = (pos == -1 ? 0 : abs(((pos + 1LL) * (long long)k) - prefix[pos])) * 1LL;\n        long long nextSum = (pos == -1 ? prefix[n - 1] : prefix[n - 1] - prefix[pos]) * 1LL;\n        long long total = (pos == -1 ? (long long)k * (n) : (long long)k * ((n - 1) - pos)) * 1LL;\n        res.push_back(currRes + abs(total - nextSum));\n    }\n    \npublic:\n    vector<long long> minOperations(vector<int>& nums, vector<int>& queries) {\n        int n = nums.size();\n        int m = queries.size();\n        sort(nums.begin(), nums.end());\n        \n        vector<long long> prefix(n);\n        long long sum = 0LL;\n        for(int i = 0; i < n; i++){\n            sum += (long long)nums[i]; // cast to long long to avoid overflow\n            prefix[i] = sum * 1LL;\n        }\n        \n        for(int i = 0; i < m; i++){\n            helper(nums, prefix, queries[i]);\n        }\n        return res;\n    }\n};\n\n```
['C++']