C++ || BINARY SEARCH || EASY TO UDERSTAND
Complexity\n- Time complexity:\nO(nlogn)\n\n- Space complexity:\nO(n)\n\n# Code\n\n#define ll long long \nclass Solution {\npublic:\n    vector<long long> minOp
# Complexity\n- Time complexity:\nO(nlogn)\n\n- Space complexity:\nO(n)\n\n# Code\n```\n#define ll long long \nclass Solution {\npublic:\n    vector<long long> minOperations(vector<int>& nums, vector<int>& queries) {\n        vector<ll> v;//ans array\n        vector<ll> ps;//previous sum array\n        sort(nums.begin(),nums.end());\n        ll sum = 0,s;\n        for(auto &i: nums){sum += i;ps.push_back(sum);}\n        int x = 0,n=nums.size();\n        for(auto &i: queries){\n            x = upper_bound(nums.begin(),nums.end(),i)-nums.begin();//binary search\n            if(x==0){\n                v.push_back((sum-n*1LL*i));\n            } else if(x==n){\n                v.push_back((x*1LL*i - ps[x-1]));\n            }else{\n                v.push_back((x*1LL*i - ps[x-1]) + (sum-ps[x-1]-i*1LL*(n-x)));\n            }\n        }\n        return v;\n    }\n};\n```
['Array', 'Binary Search', 'Sorting', 'Prefix Sum', 'C++']