#K67532. Maximum Absolute Difference After Removal
Maximum Absolute Difference After Removal
Maximum Absolute Difference After Removal
You are given an array of n integers. Your task is to remove exactly one element from the array such that the absolute difference between the maximum and minimum element in the remaining array is maximized.
Formally, let \(a_1,a_2,\ldots,a_n\) be the array. After removing one element, consider the remaining array \(a'\). You need to maximize the value of \(\max(a')-\min(a')\). Note that if the array is sorted as \(b_1 \le b_2 \le \cdots \le b_n\), one possible way to achieve a high difference is by either removing the smallest element or the largest element. However, if you remove an element that is not an extreme, the full range \(b_n - b_1\) can be maintained.
Hint: For an array sorted in non-decreasing order, the candidates for the answer are:
- \(b_n - b_1\) (if a non-extreme element is removed),
- \(b_n - b_2\) (if the smallest element is removed), and
- \(b_{n-1} - b_1\) (if the largest element is removed).
For the special case when \(n=3\), the best you can do is \(b_3 - b_1\) because removing the middle element preserves the original range.
inputFormat
The first line of input contains an integer \(T\) denoting the number of test cases. Each test case consists of two lines:
- The first line contains an integer \(n\) (\(n \ge 3\)), the number of elements in the array.
- The second line contains \(n\) space-separated integers representing the array.
Input is to be read from stdin.
outputFormat
For each test case, print a single integer — the maximum absolute difference obtainable after removing exactly one element. Each answer should be printed on a new line on stdout.
## sample2
5
1 9 3 7 4
4
5 1 5 5
8
4
</p>