#K40847. Minimal Difference with One Operation
Minimal Difference with One Operation
Minimal Difference with One Operation
You are given an array of integers. You are allowed to perform at most one operation: increment or decrement a single element by 1. Your task is to determine the minimum possible absolute difference between the maximum and minimum elements in the array after performing at most one such operation.
Let \(A = [a_1, a_2, \dots, a_n]\) be the array. Initially, the difference is \(D = \max(A) - \min(A)\). You can either decrease the maximum element by 1 or increase the minimum element by 1. Compute the new difference and output the smallest obtainable value.
Note: If the array contains only one element, the answer is 0.
inputFormat
The first line contains a single integer \(T\) representing the number of test cases. Each test case consists of two lines:
- The first line contains an integer \(N\), the number of elements in the array.
- The second line contains \(N\) space-separated integers representing the array elements.
It is guaranteed that \(N \geq 1\).
outputFormat
For each test case, output a single line containing the minimum possible absolute difference between the maximum and minimum elements of the array after performing at most one operation.
## sample3
5
1 4 2 6 8
3
10 12 15
4
5 5 5 5
6
4
0
</p>