C++✅| Binary Search | 4 liner |Easy to Understand🫡a
Intuition\n Describe your first thoughts on how to solve this problem. \nUse binary search to find the position of the element (which we want make all elements 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nUse binary search to find the position of the element (which we want make all elements to this element) in the sorted array. Then, store then answer using the formula in approach section\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n`ans.push_back(1LL * x * (2 * i - n) + prefix[n] - 2 * prefix[i]);`\n\n\n# Complexity\n- Time complexity: O(nlogn)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    vector<long long> minOperations(vector<int>& nums, vector<int>& queries) {\n        sort(nums.begin(), nums.end());\n        int n = nums.size();\n        vector<long long> ans, prefix(n + 1);\n        for (int i = 0; i < n; i++)\n            prefix[i + 1] = prefix[i] + (long long) nums[i];\n        for (int x : queries) {\n            int i = lower_bound(nums.begin(), nums.end(), x) - nums.begin();\n            ans.push_back(1LL * x * (2 * i - n) + prefix[n] - 2 * prefix[i]);\n        }\n        return ans;\n    }\n};\n```
['Array', 'Binary Search', 'Prefix Sum', 'C++']