You are given an array A of integers and an integer K, where, for each integer A[i] we need to choose either x = -K or x = K, and add x to A[i] only once. After this process, we have some array B. Your task is to find the smallest possible difference between the maximum value of B and the minimum value of B.
For Example :Input: A = [1,3,6], K = 3
Output: 3
Explanation: B = [4,6,3]
The first line of input contains a single integer ‘T’ denoting the number of test cases given.
The first line of each test case input contains two space-separated integers ‘N’ and ‘K’. Where ‘N’ denotes the number of elements in the array while ‘K’ is the value to be added or subtracted from each A[i] so that the so formed list (array) ‘B’ has the smallest possible difference between its maximum and minimum element.
The next line of input for each test case contains ‘N’ space-separated integers (elements of the list/array).
Output Format :
For every test case, print the smallest possible difference between the maximum value of B and the minimum value of B.
Print the output of each test case in a separate line.
Note :
You do not need to print anything; it has already been taken care of. Just implement the given function.
1 <= T <= 100
1 <= A.length <= 5000
0 <= A[i] <= 10000
0 <= K <= 10000
Time Limit: 1 sec
2
1 0
1
2 2
0 10
0
6
For the first test case, the given value of N = 1, while K = 0, therefore the list (array) B formed so as to result in the smallest difference between the minimum and maximum element is B = [1]. Which would give us output as 0.
While, For the next test case, the given value of N = 2 with K = 2, which would give us B = [2,8] having the smallest difference between the minimum element i.e. 2 and the maximum element i.e. 8 would give us 6.
2
3 3
1 3 6
4 2
6 7 1 3
3
2
Try and see if you could for smaller A[i], choose to increase their value ("go up"), and for bigger A[i], decrease their value ("go down").
Using the intutuin that if A[i] < A[j], we don't need to consider when A[i] goes down while A[j] goes up. Since, the interval (A[i] + K, A[j] - K) would already always be a subset of (A[i] - K, A[j] + K) (here, (a, b) for a > b denotes (b, a) instead.)
That means that it is never worse to choose (up, down) instead of (down, up).
Now, for a sorted list (array) A, assume that element A[i] is the largest i that goes up. Then, A[0] + K, A[i] + K, A[i+1] - K, A[A.length - 1] - K are the only relevant values for calculating the answer: every other value is between one of these extremal values.
Algorithm
O(N * log(N)), where ‘N’ is the length of the ‘A’.
Since we are iterating over all the elements given in the list (array) ‘A’ which is an O(N) operation but before that also we are performing a sorting on the list (array), therefore, the complexity here grows by O(N * log(N)).
O(N) or O(log(N)), where ‘N’ is the length of the ‘A’.
Here, The space complexity of the sorting algorithm depends on the implementation of each programming language.
For instance, the list.sort() function in Python is implemented with the Timsort algorithm whose space complexity is O(N).
In Java, the Arrays.sort() is implemented as a variant of quicksort algorithm whose space complexity is O(logN).