Minimum difference || easy Sliding window approach || 100% beat
Intuition
Sorting the array helps because once the array is sorted, the subarray with the smallest difference between its maximum and minimum values will be a c
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
- Sorting the array helps because once the array is sorted, the subarray with the smallest difference between its maximum and minimum values will be a contiguous block of k elements.
- By simply comparing the first and last elements of these sorted subarrays, we can efficiently find the minimum difference.
- The sliding window mechanism ensures that all possible subarrays of length k are considered without redundantly recalculating differences.

# Approach
<!-- Describe your approach to solving the problem. -->
**1. Sort the Array:** First, sort the input array nums to easily find subarrays where the difference between the maximum and minimum values is minimized.

**2 Iterate Over Subarrays:** Use a sliding window approach to examine every possible subarray of length k. For each subarray, compute the difference between its maximum and minimum (which are the last and first elements in the sorted subarray, respectively).

**3. Track Minimum Difference:** Keep track of the smallest difference encountered across all subarrays of length k.

**4. Return the Result:** Return the smallest difference found.

# Complexity
- Time complexity:O(nLogn)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:O(1)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```cpp []
class Solution {
public:
    int minimumDifference(vector<int>& nums, int k) {
        if(k == 1){
            return 0;
        }
        sort(nums.begin(),nums.end());
        int minNum = INT_MAX;
        int i = 0;
        while(i<=nums.size()-k){
            int diff = nums[i+k-1] - nums[i];
            i++;
            minNum = min(minNum , diff);
        }
        return minNum;
    }
};
```
['C++']