Minimum Difference After Changing Four Elements
Intuition\n- The goal of this function is to minimize the difference between the largest and smallest elements in the array after changing at most four elements
# Intuition\n- The goal of this function is to minimize the difference between the largest and smallest elements in the array after changing at most four elements. To do this, the function sorts the array and considers four different cases of which elements to change to minimize the difference.\n\n# Approach\n1. Check if the length of the array is 4 or less. If so, return 0 because we can make all elements equal.\n2. Sort the array in non-decreasing order to easily identify the smallest and largest elements.\n3. Consider four possible cases by calculating the differences between:\n   - The largest element and the third-largest element\n   - The second-largest element and the second-smallest element\n   - The third-smallest element and the second-smallest element\n   - The smallest element and the fourth-smallest element\n4. Return the minimum difference among these four cases.\n\n# Complexity\n- **Time complexity:** O(n log n) due to the sorting operation, where n is the length of the input array.\n- **Space complexity:** O(1). The function uses a constant amount of extra space for storing temporary variables.\n\n# Code\n```\nvar minDifference = function(nums) {\n    const n = nums.length;\n    if (n <= 4) return 0; // If there are 4 or fewer elements, we can make them all equal\n    \n    nums.sort((a, b) => a - b); // Sort the array in non-decreasing order\n    \n    // Consider the four possible cases:\n    // 1. Change the largest three elements\n    // 2. Change the smallest element and the largest two elements\n    // 3. Change the two smallest elements and the largest element\n    // 4. Change the three smallest elements\n    return Math.min(\n        nums[n - 1] - nums[3],\n        nums[n - 2] - nums[2],\n        nums[n - 3] - nums[1],\n        nums[n - 4] - nums[0]\n    );\n};\n\n```
['JavaScript']