💯✅🔥Detailed Easy Java ,Python3 ,C++ Solution|| 15 ms ||≧◠‿◠≦✌
Intuition\n Describe your first thoughts on how to solve this problem. \nThe key insights behind this intution is:\n1. If the array has less than 5 elements, th
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe key insights behind this intution is:\n1. If the array has less than 5 elements, the minimum difference will be 0, as we can\'t form two groups of 4 elements.\n1. Sorting the array in ascending order allows us to easily identify the 4 smallest and 4 largest elements.\n1. By calculating the difference between the sum of the 4 largest elements and the sum of the 4 smallest elements, we can find the minimum difference.\n\n---\n# Approach\n<!-- Describe your approach to solving the problem. -->\nThe approach used in the provided code is to find the minimum difference between the sum of the largest 4 elements and the sum of the smallest 4 elements in the given array A.\nThe intuition behind this approach is that the minimum difference will be achieved by either:\n- Removing the 4 smallest elements and keeping the 4 largest elements.\n- Removing the 4 largest elements and keeping the 4 smallest elements.\n---\n# Complexity\n- Time complexity:O(n log n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n---\n- Space complexity:O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n---\n# Step By Step Explanation\n**Let\'s go through the step-by-step explanation of how the code works using the array [5, 3, 2, 4].**\n\n- **Check if the array has less than 5 elements:**\n1. The given array A has 4 elements, which is less than 5.\n1. Therefore, the function returns 0, as per the problem statement.\n\n- **Sort the array in ascending order**:\n1. The sorted array is [2, 3, 4, 5].\n\n**Calculate the minimum difference:**\n1. The function calculates the minimum difference between the sum of the largest 4 elements and the sum of the smallest 4 elements.\n**The possible differences are:**\n- A[-1] - A: 5 - 3 = 2\n- A[-2] - A: 4 - 2 = 2\n- A[-3] - A: 3 - 3 = 0\n- A[-4] - A: 2 - 2 = 0\n\n**The minimum of these differences is 0, which is returned as the final result.**\n\n---\n# Code\n```java []\nclass Solution {\n        public int minDifference(int[] A) {\n        int n = A.length, res = Integer.MAX_VALUE;\n        if (n < 5) return 0;\n        Arrays.sort(A);\n        for (int i = 0; i < 4; ++i) {\n            res = Math.min(res, A[n - 4 + i] - A[i]);\n        }\n        return res;\n    }\n}\n```\n```python3 []\nclass Solution:\n    def minDifference(self, A: List[int]) -> int:\n        n = len(A)\n        if n < 5:\n            return 0\n        A.sort()\n        return min(A[-1] - A[3], A[-2] - A[2], A[-3] - A[1], A[-4] - A[0])\n```\n```C++ []\nclass Solution {\npublic:\n        int minDifference(vector<int>& A) {\n        int n = A.size();\n        if (n < 5) return 0;\n        sort(A.begin(), A.end());\n        return min({A[n - 1] - A[3], A[n - 2] - A[2], A[n - 3] - A[1], A[n - 4] - A[0]});\n    }\n};\n```\n---\n![upvote.png](https://assets.leetcode.com/users/images/e6f4f26f-a7f5-465b-8ec1-aa665db072f9_1719537871.6475258.png)\n\n\n\n
['Array', 'Greedy', 'Sorting', 'C++', 'Java', 'Python3']