C++, sort + sliding window, time: O(nlogn), space: O(n + logn)
\nclass Solution {\npublic:\n    \n    // sorting + sliding window\n    int minDifference(vector<int>& nums) {\n        \n        if (nums.size() < 5) {\n      
```\nclass Solution {\npublic:\n    \n    // sorting + sliding window\n    int minDifference(vector<int>& nums) {\n        \n        if (nums.size() < 5) {\n            // If the number of elements is less than or equal to four, \n            // we can always adjust the values and let the minimum difference\n            // to be zero.\n            return 0;\n        }\n        \n        // nums.size() >= 5;\n        \n        // Sort nums in ascending order.\n        sort(begin(nums), end(nums));\n        \n        // Use the idea of sliding window to find the minimum defference after three moves.\n        // Because nums is sorted, we can treat nums as circular array and choose three consecutive elements to find the answer.\n        // After we choose three consecutive elements as our window, assume the three consecutive elements are reset to the\n        // maximum value outside the window.\n        int lastIdx = nums.size() - 1;\n        \n        // Now, the first three elements are our window.\n        // The maximum value outside the window is the last element.\n        // Change the values of the first three elements to the value of nums[lastIdx].\n        int minDiff = nums[lastIdx] - nums[3];  \n        \n        // Treat nums as a circular array and choose nums[0], nums[1] and nums[lastIdx] to be our window.\n        // The maximum value outside the window is nums[lastIdx - 1].\n        // Change the values of nums[0], nums[1] and nums[lastIdx] to the value of nums[lastIdx - 1].\n        minDiff = min(minDiff, nums[lastIdx - 1] - nums[2]);\n        \n        // Based upon the idea described above, choose nums[0], nums[lastIdx] and nums[lastIdx - 1] to be our window.\n        // Change the values of nums[0], nums[lastIdx] and nums[lastIdx - 1] to the value of num[lastIdx - 2].\n        minDiff = min(minDiff, nums[lastIdx - 2] - nums[1]);\n        \n        // Based upon the idea described above, choose the last three elements to be our window.\n        // Change the values of the last three elements to the value of nums[last - 3].\n        minDiff = min(minDiff, nums[lastIdx - 3] - nums[0]);\n        \n        // Since nums has been sorted, we don\'t have to iterate through the whole array.\n        // After the silding window covers the first three and last three elements,\n        // we can find the minimum difference.\n        \n        return minDiff;\n    }\n};\n```
['C++']