Easy Java Code
Intuition\n- The task is to minimize the difference between the maximum and minimum values in an array after removing up to three elements. This can be effectiv
# Intuition\n- The task is to minimize the difference between the maximum and minimum values in an array after removing up to three elements. This can be effectively approached by considering the smallest and largest elements in a sorted array.\n\n# Approach\n1. If the length of the array is less than or equal to 4, the result is 0 because we can remove all elements or no elements, leading to a difference of 0.\n1. Sort the array.\n1. Check the difference between the highest and lowest elements after removing up to three elements from either end:\n   - Remove 3 smallest elements.\n   - Remove 2 smallest and 1 largest element.\n   - Remove 1 smallest and 2 largest elements.\n   - Remove 3 largest elements. \n4. Return the minimum difference obtained from these scenarios.\n\n# Complexity\n- Time complexity:- \uD835\uDC42( \uD835\uDC5B log\uD835\uDC5B)\n\n- Space complexity:- O(1) as only a fixed amount of extra space is used.\n\n# Code\n```\nclass Solution {\n    public int minDifference(int[] nums) {\n        int n = nums.length;\n        if (n <= 4) return 0;\n        Arrays.sort(nums);\n        int res = Integer.MAX_VALUE;\n        for (int i = 0; i <= 3; i++) {\n            res = Math.min(res, nums[n - 4 + i] - nums[i]);\n        }\n        return res;\n    }\n}\n```\n# **Conclusion**\n- By sorting the array and considering the removal of different combinations of three elements, we can effectively minimize the difference between the largest and smallest values in the modified array. This approach ensures that we evaluate all possible scenarios efficiently, leading to an optimal solution with a time complexity of O(nlogn).
['Java']