Harshit gave Aahad an array of size N and asked to minimize the difference between the maximum value and minimum value by modifying the array under the condition that each array element either increase or decrease by k(only once).
It seems difficult for Aahad so he asked for your help
Input Format
The First line contains two space-separated integers: N,K
Next lines contain N space-separated integers denoting elements of the array
Output Format
The output contains a single integer denoting the minimum difference between maximum value and the minimum value in the array
Constraints
1 =< N <= 10^5
1 <= Ai,K <= 10^9
Sample Input1:
3 6
1 15 10
Sample Output1:
5
Explaination
We change from 1 to 6, 15 to 9 and 10 to 4. Maximum difference is 5 (between 4 and 9). We can't get a lower difference.
public class Main {
public static int getMinDiff(int arr[], int n, int k) {
if (n == 1)
return 0;
Arrays.sort(arr);
// Original range
int ans = arr[n - 1] - arr[0];
int small = arr[0] + k, big = arr[n - 1] - k;
// Swap if required
if (small > big) {
int temp = small;
small = big;
big = temp;
}
for (int i = 1; i < n - 1; i++) {
// We have to do atleast one operation
int subtsract = arr[i] - k;
int add = arr[i] + k;
// If the added / substracted values are within range
if (subtsract >= small || add <= big)
continue;
// If (substracted -> big) range is smaller than (smaller -> added)
if (big - subtsract <= add - small)
small = subtsract;
else
big = add;
}
return (int) Math.min(ans, big - small);
}
}