<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>x</code>.</p>

<p>Find the <strong>minimum absolute difference</strong> between two elements in the array that are at least <code>x</code> indices apart.</p>

<p>In other words, find two indices <code>i</code> and <code>j</code> such that <code>abs(i - j) &gt;= x</code> and <code>abs(nums[i] - nums[j])</code> is minimized.</p>

<p>Return<em> an integer denoting the <strong>minimum</strong> absolute difference between two elements that are at least</em> <code>x</code> <em>indices apart</em>.</p>

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

<pre>
<strong>Input:</strong> nums = [4,3,2,4], x = 2
<strong>Output:</strong> 0
<strong>Explanation:</strong> We can select nums[0] = 4 and nums[3] = 4. 
They are at least 2 indices apart, and their absolute difference is the minimum, 0. 
It can be shown that 0 is the optimal answer.
</pre>

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

<pre>
<strong>Input:</strong> nums = [5,3,2,10,15], x = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> We can select nums[1] = 3 and nums[2] = 2.
They are at least 1 index apart, and their absolute difference is the minimum, 1.
It can be shown that 1 is the optimal answer.
</pre>

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

<pre>
<strong>Input:</strong> nums = [1,2,3,4], x = 3
<strong>Output:</strong> 3
<strong>Explanation:</strong> We can select nums[0] = 1 and nums[3] = 4.
They are at least 3 indices apart, and their absolute difference is the minimum, 3.
It can be shown that 3 is the optimal answer.
</pre>

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

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
	<li><code>0 &lt;= x &lt; nums.length</code></li>
</ul>

class Solution:
    def minAbsoluteDifference(self, nums: List[int], x: int) -> int:

        # O(n) TIME

        n=len(nums)
        # store original nums
        sorted_num= [nums[n-1]] # start with the last element of nums

        # initialize pointer at len of nums
        r = n-1
        min_val = float('inf')

        # decrement starting at the first valid index (n-1-x)
        # going in reverse to avoid unnecessary comparisons and be more memory efficient for large lists
        for l in range(n-1-x,-1,-1):

            num=nums[l]
            # find the index i where num would be inserted into a sorted list using bin search
            i=bisect.bisect_left(sorted_num,num)
            
            # check if i is in the bounds of sorted_num
            if i<len(sorted_num):
                # update min with the abs between num and element index i in sorted_num
                min_val=min(min_val,abs(num-sorted_num[i]))
            
            if i>0:
                # update min with abs between num and element before it became out of bounds 
                min_val=min(min_val,abs(num-sorted_num[i-1]))
            
            # move the right pointer down
            r-=1

            # insert the elem nums[r] into sorted_num while maintaining sorted order
            bisect.insort(sorted_num,nums[r])
        
        print(sorted_num)
        
        return min_val




        # BRUTE FORCE SOLUTION
        #min_val = float('inf')
        # for i in range(len(nums)):
        #     x_away = i + x
        #     for j in range(x_away, len(nums)):
        #         min_val = min(min_val, abs(nums[i]-nums[j]))
        
        # return min_val
31600