Image Explanation🏆- [Sorting + Prefix Sums + Binary Search] - Easy & Concise
Video Solution (Aryan Mittal)\n\nMinimum Operations to Make All Array Elements Equal by Aryan Mittal\n\n\n\n# Approach & Intution\n\n\n\n\n\n\n\n# Code\n\n#defi
# Video Solution (`Aryan Mittal`)\n\n`Minimum Operations to Make All Array Elements Equal` by `Aryan Mittal`\n![Leetcode Contest5.png](https://assets.leetcode.com/users/images/bea8dd52-30ea-4442-a9f9-a7a366152238_1679811067.7837667.png)\n\n\n# Approach & Intution\n![image.png](https://assets.leetcode.com/users/images/284b7d00-abb6-4ef9-a924-c6cdb9de9bc5_1679806649.1803951.png)\n![image.png](https://assets.leetcode.com/users/images/fe473f07-b0e2-455c-ad33-82f46a71d73f_1679806658.4850094.png)\n![image.png](https://assets.leetcode.com/users/images/a7ff49be-3116-4bed-91de-0aa42603517a_1679806665.3169062.png)\n![image.png](https://assets.leetcode.com/users/images/553fda03-cd0e-48b4-bf2b-284b9dd0d3b0_1679806672.547002.png)\n![image.png](https://assets.leetcode.com/users/images/2da0afd5-624f-4417-8d08-807ecc6d21ac_1679806685.2036808.png)\n\n\n# Code\n```\n#define ll long long int\n\nclass Solution {\npublic:\n    vector<long long> minOperations(vector<int>& nums, vector<int>& queries) {\n        \n        int n = nums.size();\n        sort(nums.begin(), nums.end());\n        vector<ll> prefSum(n+1, 0);\n        \n        for(int i=0;i<n;i++){\n            prefSum[i+1] = prefSum[i] + nums[i];\n        }\n        \n        nums.insert(nums.begin(), 0);\n        n++;\n        \n        vector<ll> ans;\n        for(auto q : queries){\n            ll lidx = lower_bound(nums.begin(), nums.end(), q) - nums.begin() - 1;\n            ll uidx = upper_bound(nums.begin(), nums.end(), q) - nums.begin();\n            \n            ll val = q*lidx - (prefSum[lidx] - prefSum[0]);\n            \n            if(uidx!=n){\n                val +=  (prefSum[n-1] - prefSum[uidx-1]) - q*(n-uidx);\n            }\n            \n            ans.push_back(val);\n        }\n    \n        return ans;\n    }\n};\n```
['Binary Search', 'Sorting', 'Prefix Sum', 'C++']