Python Complete Explanation (Prefix+suffix =3 elements)
1)Sort the array so we have a window with indicess of [0,n-1] represent [min,max].\n2)Now,changing either the left or right results in the change in the answer 
1)Sort the array so we have a window with indicess of [0,n-1] represent [min,max].\n2)Now,changing either the left or right results in the change in the answer .so,changing just means removing a number and adding a new number.We are asked change a total of 3 elements here.\n3) number of elements that can be removed from [left,right] are  [0,3],[1,2],[2,1],[3,0]\n4)when 0 elements removed from left, min remains the same,so answer = nums[-4] - nums[0]\n5)Similarly,we do for all the rest of prefix+suffix possibilities and obtain the minimum of all.\n\nConfused? why did i call changing an element as removal?\n```\neg: input = [0,1,1,4,6,6,6]\nAssume [left,right] = [2,1],Here why the answer has nothing to do with first 2 elements and last 1 element? Because,we make all of them as equal.\nExplanation:\n\t[0,1] [1,4,6,6] [6] is of form `prefix-window-suffix` so we convert all elements in prefix to window[0] and all elements in suffix to window[-1] hence maintaining the window validity to true.\n```\n`Time:O(nlogn) Space:O(1)`\n\n\tclass Solution:\n\t\tdef minDifference(self, nums: List[int]) -> int:\n\t\t\tn = len(nums)\n\t\t\tif n<=4:\n\t\t\t\treturn 0\n\t\t\tnums.sort()\n\t\t\tans = float(\'inf\')\n\t\t\ti,j = 3,n-1\n\t\t\twhile i>=0 and j>=0:\n\t\t\t\tans = min(ans,nums[j]-nums[i])\n\t\t\t\ti-=1\n\t\t\t\tj-=1\n\n\t\t\treturn ans\nThis same method works well for any general k as well. In my opinion this is also a typical sliding  window.please leave an upvote to help this post reach those trying to understand this question. :)
['Python', 'Python3']