📌📌 C++ || Sorting || Prefix Sum || Binary Search || Faster || Easy To Understand 🤷‍♂️🤷‍♂️
Using Sorting && Binary Search\n\n Time Complexity :- O(NlogN)\n\n Space Complexity :- O(N)\n\n\nclass Solution {\npublic:\n    \n    vector<long long> minOpera
* ***Using Sorting && Binary Search***\n\n* ***Time Complexity :- O(NlogN)***\n\n* ***Space Complexity :- O(N)***\n\n```\nclass Solution {\npublic:\n    \n    vector<long long> minOperations(vector<int>& nums, vector<int>& queries) {\n        \n        int n = nums.size();\n        \n        int m = queries.size();\n        \n        // sort the nums\n        \n        sort(nums.begin(), nums.end());\n        \n        // find the prefix sum array\n        \n        vector<long long> prefix(n, 0);\n        \n        prefix[0] = nums[0];\n        \n        for(int i = 1; i < n; i++)\n        {\n            prefix[i] = nums[i] + prefix[i - 1];\n        }\n        \n        long long total_sum = prefix[n - 1];\n        \n        vector<long long> ans(m);\n        \n        for(int i = 0; i < m; i++)\n        {\n            long long min_op = 0;\n            \n            int tar = queries[i];\n            \n            // find the index of the upper bound of tar\n            \n            int idx = upper_bound(nums.begin(), nums.end(), tar) - nums.begin();\n            \n            // cal. the operations required till idx - 1\n            \n            long long left_sum = 0;\n            \n            if(idx > 0)\n            {\n                left_sum += prefix[idx - 1];\n            }\n            \n            long long left_req_sum = (long long) tar * (long long) idx;\n            \n            min_op += abs(left_req_sum - left_sum);\n            \n            // cal. the operations required from idx to n - 1\n            \n            long long right_sum = total_sum;\n            \n            if(idx > 0)\n            {\n                right_sum -= prefix[idx - 1];\n            }\n            \n            long long right_req_sum = (long long) (n - idx) * (long long) tar;\n            \n            min_op += abs(right_sum - right_req_sum);\n            \n            ans[i] = min_op;\n        }\n        \n        return ans;\n    }\n};\n```
['Binary Search', 'C', 'Sorting', 'Prefix Sum', 'C++']