Runtime 235 ms Beats 100%
Code\n\n#pragma GCC optimize("Ofast","inline","fast-math","unroll-loops","no-stack-protector")\n#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,
# Code\n```\n#pragma GCC optimize("Ofast","inline","fast-math","unroll-loops","no-stack-protector")\n#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native","f16c")\n\nstatic int fast_io = []() \n{ \n    std::ios::sync_with_stdio(false); \n    cin.tie(nullptr); \n    cout.tie(nullptr); \n    return 0; \n}();\n\n#ifdef LOCAL\n    freopen("input.txt", "r" , stdin);\n    freopen("output.txt", "w", stdout);\n#endif\n\nclass Solution \n{\npublic:\n  vector<long long int>prefix;\n  vector<long long> minOperations(vector<int>& nums, vector<int>& queries) \n\t{\n\t\t  //to perform the binary search on the array we need to sort the array\n\t\t sort(nums.begin() , nums.end());\n\t\t \n         //first of all we need to do the preprocessing of the nums \n\t\t for(int i=0;i<nums.size();i++)\n\t\t {\n\t\t     if(prefix.size() == 0)\n\t\t\t {\n\t\t\t      prefix.push_back(nums[i]);\n\t\t\t }\t\n\t\t\t else\n\t\t\t {\n\t\t\t \tlong long int sum = 0LL + prefix.back() + nums[i];\n\t\t\t \tprefix.push_back(sum);\n\t\t\t }\n\t\t }\n\t\t \n\t\t //processing the each query \n\t\t vector<long long int>ans;\n\t\t for(int i=0;i<queries.size();i++)\n\t\t {\n\t\t \n\t\t \t  int ele = queries[i];\n\t\t      long long int val = binary_search_(ele,nums);\t\n\t\t      ans.push_back(val);\n\t\t }    \n\t\t return ans;\n    }\n    long long int binary_search_(int &ele,vector<int>&nums)\n    {\n    \tint start = 0;\n    \tint end   = nums.size()-1;\n    \tint pos = -1;\n    \t\n    \twhile(start <= end)\n    \t{\n    \t\tlong long int mid = (start + ((end - start)/2));\n    \t\t\n    \t\t//we need to find the last pos of the ele in the nums\n    \t    if(nums[mid] == ele)\n\t\t\t{\n\t\t\t    pos = mid;\n\t\t\t\tstart = mid+1;\t\n\t\t\t}\t\n\t\t\telse if(ele > nums[mid])\n\t\t\t{\n\t\t\t\tstart = mid+1;\n\t\t\t}\n\t\t\telse if(ele < nums[mid])\n\t\t\t{\n\t\t\t\tend = mid-1;\n\t\t\t}\n\t\t}\n\t\t\n\t\tif(pos == -1)\n\t\t{\n\t\t\tlong long int ans = 0;\n\t\t\tif(start>=0 and start<nums.size())\n\t\t\t{\n\t\t\t     long long int bigger = 0LL + ((nums.size()-1) - start + 1);\t\n\t\t\t     long long int sum    = prefix[prefix.size()+-1];\n\t\t\t     \n\t\t\t     if(start > 0) sum = sum - prefix[start-1];\n\t\t\t     \n\t\t\t     long long int val = (sum - (bigger*ele));\n\t\t\t     \n\t\t\t     ans = ans + 0LL + val;\n\t\t\t}\n\t\t\tif((end>=0 and end<nums.size()))\n\t\t\t{\n\t\t\t\t//taking out the smaller elements \n\t\t\t    long long int smaller = 0LL + (end - 0 + 1);\t\n\t\t\t    long long int sum     = 0LL + prefix[end];\n\t\t\t\tlong long int val     = 0LL + ((smaller*ele) - sum);     \n\t\t\t    ans = ans + 0LL + val;\n\t\t\t}\n\t\t\t\n\t\t\treturn ans;\n\t\t}\n\t\telse if(pos != -1)\n\t\t{\t  \n\t\t      \n\t\t      long long int ans = 0;\n\t\t      \n\t\t      long long int smaller = 0LL + pos - 0 + 1;\n\t\t      \n\t\t      //if(ele == 1) cout<<"smaller = "<<smaller<<endl;\n\t\t      \n\t\t      \n\t\t      if(smaller > 0)\n\t\t      {\n\t\t      \t\n\t\t\t  \n\t\t      long long int sum     = 0LL + prefix[pos];\n\t\t      long long int val1     = 0LL + ((smaller*ele) - sum);     \n\t\t      \n\t\t      ans = ans + 0LL + val1;\n\t\t      \n\t\t     }\n\t\t      \n\t\t       \n\t\t      long long int bigger = 0LL + ((nums.size()-1) - (pos+1) + 1);\n\t\t      \n\t\t      //if(ele == 1) cout<<"bigger = "<<bigger<<endl;\n\t\t      \n\t\t      if(bigger > 0)\n\t\t      {\n\t\t      \t\n\t\t      long long int sum    = 0LL + prefix[prefix.size()-1];\n\t\t      \n\t\t      if(pos+1-1 >= 0) sum = sum - prefix[pos+1-1];\n\t\t      long long int val2 = (sum - (bigger*ele));\n\t\t      \n\t\t      \n\t\t      \n\t\t      ans = ans + 0LL + val2;\n\t\t     }\n\t\t     //cout<<"ans = "<<ans<<endl;\n\t\t     return ans; \n\t\t}\n\t\treturn 0;\n\t}\n};\n```
['C++']