[Java] Sorting, O(NlogN)
We can sort the array and then compare every element at index i with the element at index i+k-1. Take the minimum difference of all such pairs.\n\nTime Complexi
We can sort the array and then compare every element at index `i` with the element at index `i+k-1`. Take the minimum difference of all such pairs.\n\nTime Complexity - O(NlogN)\nSpace Complexity - O(1)\n```\nclass Solution {\n    public int minimumDifference(int[] nums, int k) {\n        Arrays.sort(nums);\n        int res = Integer.MAX_VALUE;\n        for(int i=0; i<nums.length; i++){\n            if(i+k-1 >= nums.length) break;\n            res = Math.min(res, nums[i+k-1] - nums[i]);\n        }\n        return res;\n    }\n}\n```
[]