class Solution {
public:
int smallestRangeII(vector<int>& A, int K) {
sort(begin(A),end(A));
int result=A.back()-A.front();
for(int i=1;i<A.size();i++)
{
int l=min(A[0]+K,A[i]-K);
int h=max(A.back()-K,A[i-1]+K);
result = min(result,h-l);
}
return result;
}
};