Python | Greedy
see the Successfully Accepted Submission\n\n# Code\n\nclass Solution:\n    def minDifference(self, nums: List[int]) -> int:\n        if len(nums) <= 4:\n       
[see the Successfully Accepted Submission](https://leetcode.com/problems/minimum-difference-between-largest-and-smallest-value-in-three-moves/submissions/1308006366/?envType=daily-question&envId=2024-07-03)\n\n# Code\n```\nclass Solution:\n    def minDifference(self, nums: List[int]) -> int:\n        if len(nums) <= 4:\n            return 0\n        \n        nums.sort()\n        n = len(nums)\n    \n        return min(nums[n-1] - nums[3],    # Remove the three smallest elements\n                   nums[n-2] - nums[2],    # Remove the two smallest and the largest\n                   nums[n-3] - nums[1],    # Remove the smallest and the two largest\n                   nums[n-4] - nums[0])    # Remove the three largest elements\n        \n```\n\n# Step-by-Step Approach to Minimize Difference in Array\n\n## Understand the Objective\nWe want to minimize the difference between the largest and smallest values in the array. If we can change up to three elements, we should aim to either reduce the largest values, increase the smallest values, or a combination of both.\n\n## Consider Edge Cases\n- If the array has fewer than or exactly four elements, we can directly change all elements to be the same (since we can make up to three changes), resulting in a difference of 0.\n- If the array has more than four elements, we need a strategy to handle the more complex scenarios.\n\n## Sorting the Array\nSorting the array will help us easily access the largest and smallest elements that might need to be changed.\n\n## Evaluate Possible Moves\n- Change the three largest elements.\n- Change the two largest elements and one smallest element.\n- Change the one largest element and two smallest elements.\n- Change the three smallest elements.\n\nEach of these strategies will result in different maximum and minimum values, and we need to choose the one with the smallest difference.\n\n## Calculate the Minimum Difference\nAfter sorting the array, evaluate the possible minimum differences resulting from the above strategies and return the smallest one.\n\n## Explanation\n- **Edge Case Handling**: If there are fewer than or exactly four elements, we return 0 because we can always make all elements equal with at most three changes.\n- **Sorting**: We sort the array to easily access the smallest and largest elements.\n- **Evaluating Moves**: We compute the difference for the four possible strategies and return the minimum of these differences.\n\n\n![image](https://assets.leetcode.com/users/images/b1e4af48-4da1-486c-bc97-b11ae02febd0_1696186577.992237.jpeg)
['Python3']