{C++} Sliding Window, 7 LOC, clean & concise.
3 Moves 3rd of July \u2B50\n\n## Intuition\n \nSince It is a greedy problem we can easily solve it through sorting \u2705.\n\n## Approach\n Describe your approa
> ***3 Moves 3rd of July \u2B50***\n\n## Intuition\n \nSince It is a **greedy problem** we can easily solve it through **sorting** \u2705.\n\n## Approach\n<!-- Describe your approach to solving the problem. -->\n- Firstly handle the edge cases where `n<=4` as the `ans` will always be `0` for them. \n\n- Sort the vector `nums`.\n\n- Make a window of size 3.\n\n- The window will start from the right end of the vector and move towards the right too.\n\n- Check the difference b/w the Min & Max element outside the window.\n\n- After checking the difference outside all the windows return the minimum difference.\n\n\n\n<br>\n\n```cpp\nclass Solution {\npublic:\n    int minDifference(vector<int>& nums) {\n        int n = nums.size();\n        if(n<5) return 0;\n\n        sort(nums.begin(),nums.end());\n\n        int j=n-4, i = 0;\n        int ans = INT_MAX;\n\n        while(j<n) ans = min(ans,nums[j++]-nums[i++]);\n\nreturn ans;\n\n    }\n};\n```\n\n\n#### Complexity\n- Time complexity: $$O(nlogn)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(1)$$\n\n\n> #### *~ Please upvote if you found it helpful\u2764\uFE0F*\n\n<br>\n\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n![image.png](https://assets.leetcode.com/users/images/65daa3fc-c110-4738-8de2-c8a8a7f5bbe0_1719975294.853108.png)\n\n\n\n\n\n\n
['Array', 'Greedy', 'Sliding Window', 'Sorting', 'C++']