2602. Minimum Operations to Make All Array Elements Equal
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```cpp []
class Solution {
public:
    vector<long long> minOperations(vector<int>& nums, vector<int>& queries) {
        sort(nums.begin(), nums.end());
        vector<long long> prefixSum(nums.size() + 1, 0);
        
        for (int i = 0; i < nums.size(); ++i) {
            prefixSum[i + 1] = prefixSum[i] + nums[i];
        }
        
        vector<long long> result;
        
        for (int target : queries) {
            int idx = lower_bound(nums.begin(), nums.end(), target) - nums.begin();
            long long left = (long long)target * idx - prefixSum[idx];
            long long right = prefixSum[nums.size()] - prefixSum[idx] - (long long)target * (nums.size() - idx);
            result.push_back(left + right);
        }
        
        return result;
    }
};

```
['C++']