Full Detailed Explanation 🏆🏆
Intuition\nGiven the array 0 1 5 10 14, we need to remove 3 numbers to make the difference between the maximum and minimum values as small as possible. There ar
# Intuition\nGiven the array `0 1 5 10 14`, we need to remove 3 numbers to make the difference between the maximum and minimum values as small as possible. There are four possible ways to achieve this:\n\n1. Remove the first 3 elements: `? ? ? 10 14`. The difference between the maximum and minimum is `14 - 10 = 4`.\n2. Remove the last 3 elements: `0 1 ? ? ?`. The difference between the maximum and minimum is `1 - 0 = 1`.\n3. Remove the first 2 and the last 1 elements: `? ? 5 10 ?`. The difference between the maximum and minimum is `10 - 5 = 5`.\n4. Remove the first 1 and the last 2 elements: `? 1 5 ? ?`. The difference between the maximum and minimum is `5 - 1 = 4`.\n\nThe smallest difference among these scenarios is `1`. \n\n# Code Explanation \n- The variable `n` is assigned the length of the array `nums`.\n- A conditional check is performed: if the length of the array `n` is less than or equal to 4, the method returns 0 because any array with 4 or fewer elements can be made identical with at most three moves, resulting in a difference of 0.\n- The array `nums` is sorted in ascending order using `Arrays.sort(nums)`.\n- The variable `res` is initialized to `Integer.MAX_VALUE` to store the minimum difference.\n- Four potential minimum differences are calculated:\n  - The difference between the element at index `n-4` (fourth last element) and the element at index 0 (first element).\n  - The difference between the element at index `n-1` (last element) and the element at index 3 (fourth element).\n  - The difference between the element at index `n-3` (third last element) and the element at index 1 (second element).\n  - The difference between the element at index `n-2` (second last element) and the element at index 2 (third element).\n- For each of these differences, the method updates `res` with the minimum value using `Math.min`.\n- Finally, the method returns the value of `res`, which is the minimum difference between the largest and smallest values of `nums` after performing at most three moves.\n# Complexity\n- Time complexity: $$O(nlogn)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(logn)$$ because of sorting\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public int minDifference(int[] nums) {\n        int n = nums.length;\n        if (n <= 4) return 0;\n\n        Arrays.sort(nums);\n\n        int res = Integer.MAX_VALUE;\n        res = Math.min(res, nums[n-4]-nums[0]);\n        res = Math.min(res, nums[n-1]-nums[3]);\n        res = Math.min(res, nums[n-3]-nums[1]);\n        res = Math.min(res, nums[n-2]-nums[2]);\n        return res;\n    }\n}\n\n\n```\n\n\n# Follow me on LinkedIn https://www.linkedin.com/in/shivakumar139/\n# If you found this helpful, Please Upvote it \u2764\uFE0F\n\n![upvote.gif](https://assets.leetcode.com/users/images/62a36b50-cd72-48e3-bdc8-5582895ccb6e_1719987937.4340687.gif)\n
['Array', 'Greedy', 'Sorting', 'Java']