Detailed Explanation of Code and Approach
Intuition\n Describe your first thoughts on how to solve this problem. \nMy intuition is simpler just finding the number of element that are smaller than ith el
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nMy intuition is simpler just finding the number of element that are smaller than ith element of query and using prefix sum find its sum and similarly for the element that are greater than the ith element of query.Then, calculate the answer by taking difference from prefixsum and the (numberofelement * i*th* elementofquery). \n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1.Initialize variables:\n\n->m is the size of the q vector (number of queries).\n->n is the size of the nums vector (number of elements in the array).\n->ans is a vector of size m initialized with zeros, used to store the final answer for each query.\n->sum is a vector of size n initialized with zeros, used to store the prefix sum of the sorted nums vector.\n\n2.Sort the nums vector in non-decreasing order.\n\n3.Calculate the prefix sum of the sorted nums vector and store it in the sum vector.\n\n4.Iterate through each query in q:\na. Find the first element that is greater than the queried value q[i] using upper_bound from the C++ Standard Library. This function returns an iterator pointing to the first element that is greater than the given value.\nb. Calculate the index of the found element in the nums vector.\nc. Calculate the sum of the elements lower than the queried value (lower_sum) and the sum of the elements greater than the queried value (upper_sum) using the prefix sum.\nd. Calculate the total cost for both the lower elements (final_lower_sum) and the upper elements (final_upper_sum).\ne. Add both costs and store the result in the ans vector.\n\nReturn the ans vector containing the minimum total cost for each query.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(nlogn)+O(mlog(n)). \n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(n) for storing prefixsum.\n# Code\n```\n\nclass Solution {\npublic:\n    vector<long long> minOperations(vector<int>& nums, vector<int>& q) {\n    int m=q.size(),n=nums.size();\n    vector<long long> ans(m,0),sum(n,0);\n    sort(nums.begin(),nums.end());\n    sum[0]=nums[0];\n    for(int i=1;i<n;i++)\n    {\n     sum[i]=nums[i];\n     sum[i]+=sum[i-1];   \n    }\n     \n    for(int i=0;i<m;i++)\n    {\n     vector<int>::iterator upper;   \n     upper = upper_bound(nums.begin(), nums.end(), q[i]);\n     int index=upper-nums.begin();   \n     \n     long long lower_sum=0,upper_sum=0;\n     if(index!=0)   \n     lower_sum=sum[index-1];\n     \n     upper_sum=sum[n-1]-lower_sum;\n      \n     long long final_lower_sum=((1ll*q[i]*index)-lower_sum);\n     long long final_upper_sum=(upper_sum-(1ll*q[i]*(n-index)));   \n        \n     ans[i]=(final_lower_sum+final_upper_sum);   \n    }    \n      return ans;  \n    }\n};\n```
['Binary Search', 'Prefix Sum', 'C++']