C++ || SORTING || LOWER BOUND || Binary Search || Prefix Sum + BRUTE FORCE
\n\n# Complexity\n- Time complexity:\nO(m)+O(nlogN)+O(mlogN)\nFor brute force : O(mn)\n\n- Space complexity:\nO(m)\n\n# Code\n\nclass Solution {\npublic:\n#defi
\n\n# Complexity\n- Time complexity:\nO(m)+O(nlogN)+O(mlogN)\nFor brute force : O(mn)\n\n- Space complexity:\nO(m)\n\n# Code\n```\nclass Solution {\npublic:\n#define ll long long\n    vector<long long> minOperations(vector<int>& nums, vector<int>& queries) {\n        // BRUTE\n        //  ll n = nums.size();\n        //  ll m = queries.size();\n        //  vector<ll> ans(m, 0);\n        //  for (ll i = 0; i < m; i++) {\n        //      ll data = queries[i];\n        //      ll change = 0;\n        //      for (ll j = 0; j < n; j++) {\n        //          change += abs(nums[j] - data);\n        //      }\n        //      ans[i] = change;\n        //  }\n        //  return ans;\n        sort(nums.begin(), nums.end());\n        ll n = nums.size();\n        ll m = queries.size();\n        vector<ll> prefix(n), ans(m);\n        prefix[0] = nums[0];\n        for (ll i = 1; i < n; i++) {\n            prefix[i] = prefix[i - 1] + nums[i];\n        }\n        for (ll i = 0; i < m; i++) {\n            ll res = 0;\n            ll data = queries[i];\n            int lb = lower_bound(nums.begin(), nums.end(), data) - nums.begin();\n            if (lb > 0) {\n                res += data * lb - prefix[lb - 1];\n            }\n\n            if (lb < n) {\n                res += (prefix[n - 1] - (lb > 0 ? prefix[lb - 1] : 0)) -\n                       (n - lb) * data;\n            }\n\n            ans[i] = res;\n        }\n        return ans;\n    }\n};\n```
['C++']