1
// Need to consider each ith value as boundary and considering that,2
// the max value can be max val - k or i-1 th value + k3
// similarly the mn value can be min val + k or ith value - k4
// Need to check the difference of max min for all ith positions and minimize5
// this. Also check if the calculate min is larger than initial min or not12
here min value will be 9 -2 = 7 when i = 2 but intital diff between min and max13
is 6, so the answer will be 6.18
int smallestRangeII(vector<int> &nums, int k) {19
sort(nums.begin(), nums.end());20
int mn = nums.front();22
if (mx - mn <= k) return mx - mn;23
int newMn = INT_MAX, newMx = INT_MIN;27
for (int i = 1; i < nums.size(); ++i) {28
newMn = min(mn, nums[i] - k);29
newMx = max(mx, nums[i - 1] + k);30
ans = min(ans, newMx - newMn);