C++ Solution || Using Sorting || Clear Code
Using Bubble and Selection Sort\n\nclass Solution {\npublic:\n    int minDifference(vector<int>& nums) {\n        int n = nums.size();\n        if(n <= 3) retur
**Using Bubble and Selection Sort**\n```\nclass Solution {\npublic:\n    int minDifference(vector<int>& nums) {\n        int n = nums.size();\n        if(n <= 3) return 0;\n        \n        //using bubble sort (4 greatest elements will go at its right position)\n        for(int i = 0; i < 4; i++)\n            for(int j = 0; j < n-1-i; j++)\n                if(nums[j] > nums[j+1]) swap(nums[j], nums[j+1]);\n        \n        //using selection sort (4 smallest elements will go at its right position)\n        int mini, index;\n       for(int i = 0; i < 4; i++){\n           mini = nums[i], index = i;\n           for(int j = i+1; j < n; j++){\n               if(mini > nums[j]){\n                   mini = nums[j];\n                   index = j;\n               }\n           }\n           swap(nums[i], nums[index]);\n       }\n        \n        int first = nums[n-1] - nums[3]; //remove first 3 elements\n        int second = nums[n-4] - nums[0]; //remove last 3 elements\n        int third = nums[n-2] - nums[2]; //remove first 2 and 1 last elements\n        int forth = nums[n-3] - nums[1]; //remove first 1 and 2 last elements\n        \n        return min(min(first, second), min(third, forth));\n    }\n};\n```\n\n**Using STL sort function**\n```\nclass Solution {\npublic:\n    int minDifference(vector<int>& nums) {\n        int n = nums.size();\n        if(n <= 3) return 0;\n        sort(nums.begin(), nums.end());\n        \n        int first = nums[n-1] - nums[3]; //remove first 3 elements\n        int second = nums[n-4] - nums[0]; //remove last 3 elements\n        int third = nums[n-2] - nums[2]; //remove first 2 and 1 last elements\n        int forth = nums[n-3] - nums[1]; //remove first 1 and 2 last elements\n        \n        return min(min(first, second), min(third, forth));\n    }\n};\n```
['C', 'Sorting', 'C++']