C++
Complexity\n- Time complexity:O(n log n)\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
# Complexity\n- Time complexity:`O(n log n)`\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        \n        int n = nums.size();\n        int q = queries.size();\n\n        // SORT THE ARRAY\n        sort(nums.begin(),nums.end());\n\n        // CALCULATE PREFIX OF NUMS \n        vector<long long>prefix(n,0);\n        prefix[0] = nums[0];\n        for(int i=1;i<n;i++) prefix[i] = prefix[i-1] + nums[i];\n        \n        // USE LONG LONG FOR AVOID RUNTIME\n        vector<long long>ans;\n\n        long long increment = 0 , decrement = 0;\n        long long value = 0;\n\n        // processing each query \n        for(auto q : queries){\n            \n            // binary search\n            int lo = 0 , hi = n-1;\n            int mid; value = q;\n\n            // find position of current query element in sorted nums\n            while(lo <= hi){\n\n                mid = lo + (hi - lo)/2;\n\n                if(nums[mid] <= value) lo = mid + 1;\n                else hi = mid - 1;\n                \n            }\n\n            // reset increment & decrement\n            increment = 0 , decrement = 0;\n\n            // get length of less than current query element which are need to increment\n            if(lo > 0) increment = abs((value * lo) - prefix[lo-1]);\n\n            // get length of greater than current query element which are need to decrement\n            if(lo == 0) decrement = abs((value*n) - prefix[n-1]);\n            else decrement = abs((value * (n-lo)) - (prefix[n-1] - prefix[lo-1]));\n\n            // store answerr for current query\n            ans.push_back(increment + decrement);\n        }\n        \n        // return result\n        return ans;\n    }\n};\n```
['Array', 'Binary Search', 'Sorting', 'Prefix Sum', 'C++']