Simple Code C++
Approach and Intuition\nProblem Understanding:\nThe problem asks for the minimum difference between the largest and smallest numbers in the array after making a
# Approach and Intuition\nProblem Understanding:\nThe problem asks for the minimum difference between the largest and smallest numbers in the array after making at most three changes to the array. The changes are essentially removing up to three elements from the array to minimize the difference between the remaining elements.\n\nIntuition:\nIf we sort the array, the smallest possible difference between the maximum and minimum values in the array will be among the following four scenarios:\n\nRemove the three largest elements.\nRemove the two largest elements and the smallest element.\nRemove the largest element and the two smallest elements.\nRemove the three smallest elements.\nIn each case, the resulting array will be shorter by three elements, so we can find the difference between the smallest and largest numbers in the remaining array. Sorting the array helps us easily access these candidates for removal.\n\nDetailed Steps:\nCheck if the array size is less than or equal to 4. If it is, we can remove all elements, resulting in a minimum difference of 0.\nSort the array.\nCalculate the difference in the following scenarios:\nRemove the three largest elements: nums[n-4] - nums[0]\nRemove the two largest and the smallest element: nums[n-3] - nums[1]\nRemove the largest and the two smallest elements: nums[n-2] - nums[2]\nRemove the three smallest elements: nums[n-1] - nums[3]\nReturn the minimum of these differences.\n\n# Complexity\n- Time complexity:\nSorting the array takes O(nlogn)\nFinding the minimum difference among four candidates takes O(1)\n\nThus, the overall time complexity is O(nlogn).\n\n- Space complexity:\nO(1)\n\n# Code\n```\nclass Solution {\npublic:\n    int minDifference(vector<int>& nums) {\n    if(nums.size()<=4)return 0;\n    int n = nums.size();\n     sort(nums.begin(),nums.end());\n    return min((nums[n-4]-nums[0]),min((nums[n-3]-nums[1]),min((nums[n-2]-nums[2]),(nums[n-1]-nums[3]))\n    ));\n\n    }\n};\n```
['Array', 'Greedy', 'Sorting', 'C++']