Easy to Understand ! Beginners Friendly
IntuitionApproachFor Each query find the last index of that element in the nums .
The Elements left side of that index are smaller than Query[i] they should be
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->
For Each query find the last index of that element in the nums .
The Elements left side of that index are smaller than Query[i] they should be increased
The Elements right side of that index are greater than Query[i] they should be decreased
Edge case-- idx==0 || idx==n


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

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

# Code
```cpp []
class Solution {
public:
    vector<long long> minOperations(vector<int>& nums, vector<int>& queries) {
        vector<long long> ans;
        long long n=nums.size();
        int m=queries.size();
        sort(nums.begin(),nums.end());
        vector<long long> prefixsum(n,0);
        long long pre=0;
        for(int i=0;i<n;i++){
            pre+=nums[i];
            prefixsum[i]=pre;
        }
        for(int i=0;i<m;i++){
        long long idx=lower_bound(nums.begin(),nums.end(),queries[i]+1)-nums.begin();
        long long g=queries[i];
          if(idx==n || idx==0){
            long long k=abs(n*g-prefixsum[n-1]);
            ans.push_back(k);
            continue;
          }
          idx=idx-1;
          long long a=g*(idx+1)-prefixsum[idx];
          a+=(prefixsum[n-1]-prefixsum[idx])-(n-idx-1)*g;
          ans.push_back(a);

        }
        return ans;
    }
};
```
['Array', 'Binary Search', 'Sorting', 'Prefix Sum', 'C++']