Simple solution along with Follow up for 'K'  | Java | C++ | Python
Intuition\nFirst lets understand for k = 3\nYou can remove in this manner \n1. Remove first three and last zero elements (3,0) --> nums[n-1] - nums[3]\n2. Remov
# Intuition\nFirst lets understand for k = 3\nYou can remove in this manner \n1. Remove first three and last zero elements (3,0) --> nums[n-1] - nums[3]\n2. Remove first two and last one (2,1) --> nums[n-2] - nums[2]\n3. Remove first one and last two (1,2) --> nums[n-3] - nums[1]\n4. Remove first zero and last three elements (0,3) --> nums[n-4] - nums[0]\n\nSo we take the minimum of these. \n\nSo for k we can solve like this\n1. remove (k,0)\n2. remove (k-1,1)\n3. remove (k-2,2)\n4. so on..... \n\n\n# Approach\n\nWe will use sliding window technique to solve for k\nIf you want to know the reference for this idea checkout this problem [https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/description/]()\n\nNow for the first k we will solve and then slide as shown\n\n![Image.jpg](https://assets.leetcode.com/users/images/67118654-559d-4815-9593-17104d0d38eb_1719983272.8709147.jpeg)\n\n\n# Complexity\n- Time complexity:\n$$O(nlogn)$$\n\n- Space complexity:\n$$O(1)$$\n\n# Code\n```java []\nclass Solution {\n    public int minDifference(int[] nums) {\n        return solve(nums,3);        \n    }\n    public int solve(int[] nums,int k){\n        int n = nums.length;\n        Arrays.sort(nums);\n        if(n<=k){\n            return 0;\n        }\n        int i = k, j = n-1;\n        int ans = Integer.MAX_VALUE;\n        while(i>=0){\n            ans = Math.min(nums[j]-nums[i],ans);\n            i--;\n            j--;\n        }\n        return ans;\n    }\n}\n\n```\n```python []\nclass Solution:\n    def minDifference(self, nums):\n        return self.solve(nums, 3)\n\n    def solve(self, nums, k):\n        nums.sort()\n        n = len(nums)\n        if n <= k:\n            return 0\n        i = k\n        j = n - 1\n        ans = float(\'inf\')\n        while i >= 0:\n            ans = min(nums[j] - nums[i], ans)\n            i -= 1\n            j -= 1\n        return ans\n```\n```C++ []\n#include <vector>\n#include <algorithm>\n#include <climits>\n\nclass Solution {\npublic:\n    int minDifference(std::vector<int>& nums) {\n        return solve(nums, 3);\n    }\n\n    int solve(std::vector<int>& nums, int k) {\n        std::sort(nums.begin(), nums.end());\n        int n = nums.size();\n        if (n <= k) {\n            return 0;\n        }\n        int i = k, j = n - 1;\n        int ans = INT_MAX;\n        while (i >= 0) {\n            ans = std::min(nums[j] - nums[i], ans);\n            i--;\n            j--;\n        }\n        return ans;\n    }\n};\n```\n\n
['Array', 'Two Pointers', 'Greedy', 'Sliding Window', 'Sorting', 'Python', 'C++', 'Java', 'Python3']