<p>You are given two positive integer arrays <code>nums1</code> and <code>nums2</code>, both of length <code>n</code>.</p>

<p>The <strong>absolute sum difference</strong> of arrays <code>nums1</code> and <code>nums2</code> is defined as the <strong>sum</strong> of <code>|nums1[i] - nums2[i]|</code> for each <code>0 &lt;= i &lt; n</code> (<strong>0-indexed</strong>).</p>

<p>You can replace <strong>at most one</strong> element of <code>nums1</code> with <strong>any</strong> other element in <code>nums1</code> to <strong>minimize</strong> the absolute sum difference.</p>

<p>Return the <em>minimum absolute sum difference <strong>after</strong> replacing at most one<strong> </strong>element in the array <code>nums1</code>.</em> Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>

<p><code>|x|</code> is defined as:</p>

<ul>
	<li><code>x</code> if <code>x &gt;= 0</code>, or</li>
	<li><code>-x</code> if <code>x &lt; 0</code>.</li>
</ul>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums1 = [1,7,5], nums2 = [2,3,5]
<strong>Output:</strong> 3
<strong>Explanation: </strong>There are two possible optimal solutions:
- Replace the second element with the first: [1,<u><strong>7</strong></u>,5] =&gt; [1,<u><strong>1</strong></u>,5], or
- Replace the second element with the third: [1,<u><strong>7</strong></u>,5] =&gt; [1,<u><strong>5</strong></u>,5].
Both will yield an absolute sum difference of <code>|1-2| + (|1-3| or |5-3|) + |5-5| = </code>3.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums1 = [2,4,6,8,10], nums2 = [2,4,6,8,10]
<strong>Output:</strong> 0
<strong>Explanation: </strong>nums1 is equal to nums2 so no replacement is needed. This will result in an 
absolute sum difference of 0.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums1 = [1,10,4,4,2,7], nums2 = [9,3,5,1,7,4]
<strong>Output:</strong> 20
<strong>Explanation: </strong>Replace the first element with the second: [<u><strong>1</strong></u>,10,4,4,2,7] =&gt; [<u><strong>10</strong></u>,10,4,4,2,7].
This yields an absolute sum difference of <code>|10-9| + |10-3| + |4-5| + |4-1| + |2-7| + |7-4| = 20</code>
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == nums1.length</code></li>
	<li><code>n == nums2.length</code></li>
	<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums1[i], nums2[i] &lt;= 10<sup>5</sup></code></li>
</ul>

class Solution:
    def minAbsoluteSumDiff(self, nums1: List[int], nums2: List[int]) -> int:
        n = len(nums1)
        res = math.inf
        mod = 10**9 + 7
        # sum(nums1[i]-nums2[i]) for each i
        # we can raplace at most one elment in numbs1 with any other elemnt in nums1 to minimize the abs difference
        # what's the minimum after replacing one elem


        # we want to find the the largest difference,
        # and replace the first number with the one 



        # what to replace it with, we want to replace it with the closest guy to the nums2
        # first we iterate, and calc the diff, and i for max difference
        # the we go over each element in nums 1

        # sort
        # we go through each, we replace and we test
        

        total = sum([abs(nums1[i]-nums2[i]) for i in range(n)])
        res = total

        nums = list(zip(nums1, nums2))
        nums.sort(key=lambda x:x[0])
        
        # for each pair, try to find closest to nums2 in nums1, and check the total

        def get_closest(x):
            low, high = 0, n-1
            res = math.inf

            while low <= high: 
                mid = (low + high) >> 1
                if nums[mid][0] == x:
                    return x
    
                if nums[mid][0] > x:
                    high = mid - 1
                    if abs(nums[mid][0] - x) < abs(res - x):
                        res = nums[mid][0]
                else:
                    low = mid + 1
                    if abs(nums[mid][0] - x) < abs(res - x):
                        res = nums[mid][0]

            return res

        for x1,x2 in nums:
            # get closest
            x = get_closest(x2)
            
            res = min(res, total - abs(x1-x2)+abs(x-x2))
        return res%mod
36500