Simplest C++ Solution | Explanation | Clean Code |
We had to replace three element in the array to make the amplitude (difference between max and min) minimum.\nWe can choose these elements from anywhere but to 
We had to replace three element in the array to make the amplitude (difference between max and min) minimum.\nWe can choose these elements from anywhere but to minimize the amplitude, we have to choose them from either ends of the array.\nSo we have following possiblities:\n1. 0 start + 3 end\n2. 1 start + 2 end\n3. 2 start + 1 end\n4. 3 start + 0 end\n\nIf number of element is <= 4, we can always choose 3 from starting or ending and make the amplitude zero. So this becomes our edge case.\n\nWe simple use a for loop.\n\n```\nclass Solution {\npublic:\n    int minDifference(vector<int>& arr) {\n        if(arr.size() <= 4) return 0;\n        int res = INT_MAX;\n        sort(arr.begin(), arr.end());\n        for(int i = 0; i <= 3; i++)\n        {\n            res = min(res,(arr[arr.size()-4+i] - arr[i]));\n        }\n        return res;\n    }\n};\n```\nDo upvote if it helped you!
['C', 'Sorting']