C++ | EASY TO UNDERSTAND WITH EXPLAINATION
Intuition\n Describe your first thoughts on how to solve this problem. \nSorting the array seems like a sensible approach to finding the minimum difference betw
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nSorting the array seems like a sensible approach to finding the minimum difference between elements in the array. With the sorted array, we can then iterate through sliding windows of size k and find the minimum difference within these windows.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nSort the input array nums.\nInitialize two pointers, left and right, to denote the start and end of the sliding window respectively. Set left = 0 and right = k - 1.\nInitialize a variable res to store the minimum difference, initially set to INT_MAX.\nIterate through the array using the sliding window approach:\nCalculate the difference between the element at index right and the element at index left.\nUpdate res with the minimum of its current value and the calculated difference.\nMove both left and right pointers to the right by one step.\nReturn the minimum difference res.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nSorting the array takes O(n log n) time.\nIterating through the array with the sliding window approach takes O(n) time.\nSo, the overall time complexity is O(n log n).\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nSorting the array in-place takes O(1) space.\nThe additional space used is O(1).\nSo, the overall space complexity is O(1).\n\n# Code\n```\nclass Solution {\npublic:\n    int minimumDifference(vector<int>& nums, int k) {\n        \n\n        sort(nums.begin(),nums.end());\n        int left = 0;\n        int right = k-1;\n        int res = INT_MAX;\n\n        while(right<nums.size()){\n            res = min(res,nums[right] - nums[left]);\n            left++;\n            right++;\n        }\n\n        return res;\n\n    }\n};\n\n\n\n```
['Python', 'C++', 'Java']