# 1818

Apr 4th, 2021
92
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. class Solution {
2. public:
3.     int minAbsoluteSumDiff(vector<int>& nums1, vector<int>& nums2) {
4.         long long int ans, diff, e, sz, low, high;
5.         long long int mod = 1000000007LL;
6.         vector<int> v;
7.         ans = 0;
8.         sz = nums2.size();
9.         for(int i=0; i<sz; i++){
10.             diff = abs(nums1[i]-nums2[i]);
11.             ans = (ans+diff);
12.             v.push_back(diff);
13.         }
14.         long long int nans = ans;
15.         sort(nums1.begin(), nums1.end());
16.         for(int i=0; i<sz; i++){
17.             high = lower_bound(nums1.begin(), nums1.end(), nums2[i])-nums1.begin();
18.             low = high-1;
19.             if(low>=0 && low<sz){
20.                 e = nums1[low];
21.                 diff = abs(nums2[i]-e);
22.                 nans = min(nans, (ans-v[i]+diff));
23.             }
24.             if(high<sz && high>=0){
25.                 e = nums1[high];
26.                 diff = abs(nums2[i]-e);
27.                 nans = min(nans, (ans-v[i]+diff));
28.             }
29.         }
30.         return (nans%mod);
31.     }
32. };
RAW Paste Data