C++ Sorting + Binary Search + Prefix Sum || Easy To Understand ⭐⭐⭐
Intuition\n Describe your first thoughts on how to solve this problem. \nPartition the array in set of elements greeater than queries[i] and set of elements sma
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nPartition the array in set of elements greeater than queries[i] and set of elements smaller than queries[i]\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\nAfter partitioning \nFor smaller set \nrequired value to be added = (no.of elements * queries[i])- (sum of all elements)\nFor greater set \nrequired value to be added = (sum of all elements)-(no.of elements * queries[i])\nadd both values and push into answer vector\n\nDo above process m query length times  \n\n\n# Complexity\n- Time complexity:O(nlogn + mlogn + n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(n) Prefix array \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        long long n=nums.size(), m=queries.size();\n\n        vector<long long> ans;\n        sort(nums.begin(), nums.end());\n\n        vector<long long> prefix(n);\n        for(int i=0;i<n;i++) prefix[i]=(long long)nums[i];\n        for(int i=1;i<n;i++){\n            prefix[i]+=prefix[i-1];\n        }\n\n        for(int i=0;i<queries.size();i++){\n            long long value=queries[i];\n            int index=-1;\n            \n            long long low=0, high=n-1;\n            while(low<=high){\n                long long mid=low+(high-low)/2;\n                if(nums[mid]<=value){\n                    index=mid;\n                    low=mid+1;\n                }\n                else high=mid-1;          \n            }\n            if(index==-1){\n                ans.push_back(prefix[n-1]-(value*n));\n            }\n            else{\n                long long req1=(index+1)*value-prefix[index];\n                long long req2=(prefix[n-1]-prefix[index])-(n-index-1)*value;\n                ans.push_back(req1+req2);               \n            }\n        }\n        return ans;        \n    }\n};\n```
['C++']