99% Running time | SLIDING WINDOW | Easy to understand
Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem asks to minimize the difference between the maximum and minimum values of a
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem asks to minimize the difference between the maximum and minimum values of an array after making at most three moves. Each move can change any one element to any other value. If we can make at most three moves, the optimal strategy involves focusing on the smallest and largest elements, as these contribute the most to the difference.\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nSort the Array: Sorting helps in easily accessing the smallest and largest elements.\nHandle Small Arrays: If the array has four or fewer elements, the difference can always be reduced to zero since we can change each element in three moves or less.\nEvaluate Possible Changes: For arrays with more than four elements, consider the four smallest and four largest elements. By examining different combinations of three moves on these elements, we can determine the minimum possible difference.\nWe can consider removing combinations of three elements from the start or end, or a mix of both.\nCalculate the Differences: Calculate the differences between appropriate elements after removing three elements from consideration.\n# Complexity\n- Time complexity:O(nlogn)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution(object):\n    def minDifference(self, nums):\n        """\n        :type nums: List[int]\n        :rtype: int\n        """\n        #iwthin 3 moves\n        # every move can change 1 val to any val\n        n = len(nums)\n        if n <= 4:\n            return 0\n        nums.sort()\n        count = 0\n        minGap = float("inf")\n        mergedList = []\n        if n == 5:\n            for i in range(n - 3, n):\n                mergedList.append((i, nums[i]))\n            for i in range(2):\n                mergedList.append((i, nums[i]))\n        else:\n            for i in range(n - 3, n):\n                mergedList.append((i, nums[i]))\n            for i in range(3):\n                mergedList.append((i, nums[i]))\n                \n        i, j = 0, 2\n        res = []\n        while j < len(mergedList):\n            firstPosition, secondPosition = mergedList[i][0], mergedList[j][0]\n            res *= 0\n            if firstPosition == n - 3 and secondPosition == n - 1:\n                res = nums[: n - 3]\n            elif firstPosition == 0 and secondPosition == 2:\n                res = nums[3:]\n            else:\n                minPosition = min(firstPosition, secondPosition)\n                maxPosition = max(firstPosition, secondPosition)\n                res = nums[minPosition + 1:maxPosition]\n            gap = abs(res[-1] - res[0])\n            minGap = min(gap, minGap)\n            i += 1\n            j += 1\n        return minGap\n```\n\n![poor_broke_emoji.jpg](https://assets.leetcode.com/users/images/d1603fbb-9296-45af-b9bd-dde7d99d24a2_1720102833.8280423.jpeg)\n
['Python']