Given an array A of integers, for each integer A[i] we may choose any x with -K <= x <= K, and add x to A[i].
After this process, we have some array B.
Return the smallest possible difference between the maximum value of B and the minimum value of B.
 



Example 1:
Input: A = [1], K = 0
Output: 0
Explanation: B = [1]


Example 2:
Input: A = [0,10], K = 2
Output: 6
Explanation: B = [2,8]


Example 3:
Input: A = [1,3,6], K = 3
Output: 0
Explanation: B = [3,3,3] or B = [4,4,4]

 
Note:

1 <= A.length <= 10000
0 <= A[i] <= 10000
0 <= K <= 10000

Difficulty: introductory

Starter code:

class Solution:
    def smallestRangeI(self, A: List[int], K: int) -> int:
        

Solutions:
class Solution:
    def smallestRangeI(self, A: List[int], K: int) -> int:
        if len(A) == 1:
            return 0
        _min = min(A)
        _max = max(A)
        if _max - K <= _min + K:
            return 0
        return _max - _min - 2*K

class Solution:
    def smallestRangeI(self, A: List[int], K: int) -> int:
        if (min(A)+abs(K)) > (max(A)-abs(K)):
            return 0
        else:
            return (max(A)-abs(K))-(min(A)+abs(K))

class Solution:
    def smallestRangeI(self, A: List[int], K: int) -> int:
        return max(max(A)-min(A)-2*K, 0)

class Solution:
    def smallestRangeI(self, A: List[int], K: int) -> int:
        small,large = 10001,-1
        for x in A:
            small,large = min(x,small),max(x,large)
        return max(large - small - 2*K,0)

class Solution:
    def smallestRangeI(self, A: List[int], K: int) -> int:
        Max = max(A)
        Min = min(A)
        res = (Max - K) - (Min + K)
        if res < 0:
            return 0
        return res

class Solution:
    def smallestRangeI(self, A: List[int], K: int) -> int:
        return max((max(A)-K) - (min(A)+K), 0)

class Solution:
    def smallestRangeI(self, A: List[int], K: int) -> int:
        return max(max(A) - min(A) - 2*K, 0)


class Solution:
    def smallestRangeI(self, A: List[int], K: int) -> int:
        min_val = max_val = A[0]
        
        for a in A:
            if a < min_val: min_val = a
            elif a > max_val: max_val = a
            # min_val = min(min_val, a)
            # max_val = max(max_val, a)
        
        return max(max_val - min_val - 2 * K, 0)  
        


class Solution:
    def smallestRangeI(self, A: List[int], K: int) -> int:
        avg = round((min(A) + max(A)) / 2)
        mn, mx = float('Inf'), float('-Inf')
        for n in A:
            d = min(K, abs(avg-n))
            n += d * (-1 if n > avg else 1)
            mn, mx = min(mn, n), max(mx, n)

        return mx - mn


class Solution:
    def smallestRangeI(self, A: List[int], K: int) -> int:
        A.sort()
        return A[-1] - A[0] - 2 * K if A[-1] - A[0] > 2 * K else 0

class Solution:
    def smallestRangeI(self, A: List[int], K: int) -> int:
        a, b = max(A), min(A)
        diff = a - b
        coverage = 2 * K
        if diff <= coverage:
            return 0
        else:
            return diff - coverage
        
                
                


class Solution:
    def smallestRangeI(self, A: List[int], K: int) -> int:
        mx, mn = max(A), min(A)

        return max(mx - mn - 2 * K, 0)


class Solution:
    def smallestRangeI(self, A: List[int], K: int) -> int:
        return max(0, max(A)-min(A)-2*K)

class Solution:
    def smallestRangeI(self, A: List[int], K: int) -> int:
        v_min, v_max = min(A), max(A)
        if v_max - v_min >= 2*K:
            return v_max - v_min - 2*K
        else:
            return 0

class Solution:
    def smallestRangeI(self, A: List[int], K: int) -> int:
        M, m = max(A), min(A)
        if M-m <= 2*K:
            return 0
        else:
            return (M-K)-(m+K)

class Solution:
    def smallestRangeI(self, A: List[int], K: int) -> int:
        if (max(A)-min(A)) > K*2:
            return max(A)-min(A) - K*2
        else:
            return 0

class Solution:
    def smallestRangeI(self, A: List[int], K: int) -> int:
        max_value = max(A) - K
        min_value = min(A) + K
        if max_value > min_value:
            return max_value - min_value
        else:
            return 0

class Solution:
    def smallestRangeI(self, A: List[int], K: int) -> int:
        min_element, max_element = min(A), max(A)
        return max(0, max_element - min_element - 2*K)
                
                
        


class Solution:
    def smallestRangeI(self, A: List[int], K: int) -> int:
        return max(0, max(A) - min(A) - 2 * K)


class Solution:
    def smallestRangeI(self, A: List[int], K: int) -> int:
        return max(max(A) - min(A) - 2 * K, 0)
#         min_val = max_val = A[0]
        
#         for a in A:
#             if a < min_val: min_val = a
#             elif a > max_val: max_val = a
#             # min_val = min(min_val, a)
#             # max_val = max(max_val, a)
        
#         return max(max_val - min_val - 2 * K, 0)  
        


class Solution:
    def smallestRangeI(self, A: List[int], K: int) -> int:
        mx = max(A)
        mi = min(A)
        return 0 if mx - mi <= 2*K else mx - mi - 2*K

class Solution:
    def smallestRangeI(self, A: List[int], K: int) -> int:
        return max(0,max(A)-min(A)-2*K)


class Solution:
    def smallestRangeI(self, A: List[int], K: int) -> int:
        ks = [K for a in A]
        ks_left = True
        last_change = 0
        while ks_left and max(A) - min(A) > 0 and (max(A) - min(A)) != last_change:
            last_change = max(A) - min(A)
            # print(ks, A)
            ks_left = False
            average = sum(A) / len(A)
            for i in range(len(A)):
                difference = A[i] - average
                if -ks[i] <= difference <= ks[i]:
                    A[i] = average
                    # print(A[i], ks[i], difference)
                    ks[i] = abs(ks[i] - abs(difference))
                    # print(A[i], ks[i], difference)
                elif -ks[i] > difference:
                    A[i] += ks[i]
                    ks[i] = 0
                else:
                    A[i] -= ks[i]
                    ks[i] = 0
                if ks[i] > 0:
                    ks_left = True
        print((ks, A))
        return int(max(A) - min(A))
        
        # if K == 0:
        #     return max(A) - min(A)
        # max_A = max(A)
        # max_I = A.index(max_A)
        # min_A = min(A)
        # min_I = A.index(min_A)
        # if -K <= max_A - min_A <= K:
        #     A[max_I] -= (max_A - min_A) / 2
        #     A[min_I] += (max_A - min_A) / 2


class Solution:
    def smallestRangeI(self, A: List[int], K: int) -> int:
        A = sorted(A)
        min1 = set()
        max1 = set()
        for i in range(K+1):
            min1.add(A[0]+i)
        
        for i in range(K,-1,-1):
            max1.add(A[-1]-i)
            
        #print(min1,max1)
        
        s1 = min1 & max1
        if len(s1) > 0:
            return 0
        else:
            return min(list(max1)) - max(list(min1))


class Solution:
    def smallestRangeI(self, A: List[int], K: int) -> int:
        dist = max(A) - min(A)
        return dist - 2 * K if dist > 2 * K else 0

class Solution:
    def smallestRangeI(self, a: List[int], k: int) -> int:
        a = sorted(a)
        a[-1] = max(a)
        a[0]= min(a)
        #print(a,a[-1])
        if a[-1]-k<=(a[0]+k):
            return 0
        return  (a[-1]-k)-(a[0]+k)


class Solution:
    def smallestRangeI(self, a: List[int], k: int) -> int:
        a = sorted(a)
        #print(a,a[-1])
        if a[-1]-k<=(a[0]+k):
            return 0
        return  (a[-1]-k)-(a[0]+k)


class Solution:
    def smallestRangeI(self, A: List[int], K: int) -> int:
        if K == 0:
            return max(A) - min(A)
        # if len(set(A)) == 1:
        #     return 0
        mean = (max(A) + min(A)) // 2
        B = []
        for num in A:
            if num + K <= mean:
                B.append(num + K)
            elif num - K >= mean:
                B.append(num - K)
            else:
                B.append(mean)
        return max(B) - min(B)

class Solution:
    def smallestRangeI(self, A: List[int], K: int) -> int:
        if K == 0:
            return max(A) - min(A)
        if len(set(A)) == 1:
            return 0
        mean = (max(A) + min(A)) // 2
        B = []
        for num in A:
            if num + K < mean:
                B.append(num + K)
            elif num - K > mean:
                B.append(num - K)
            else:
                B.append(mean)
        return max(B) - min(B)