#K50082. Minimize Tower Height Difference
Minimize Tower Height Difference
Minimize Tower Height Difference
You are given an array of integers representing the heights of towers and an integer \(K\). For each tower, you can either add or subtract \(K\) from its height exactly once. Your task is to determine the minimum possible difference between the height of the tallest tower and the height of the shortest tower after all modifications.
Formally, given an array \(A = [a_1, a_2, \dots, a_n]\), you can transform each element \(a_i\) into either \(a_i + K\) or \(a_i - K\). Let \(A'\) denote the resulting array after the modifications. You need to compute the minimum possible value of \(D = \max(A') - \min(A')\).
Note that if the array is empty or contains a single element, the difference is \(0\).
inputFormat
The input is given via standard input (stdin) and consists of three lines:
- The first line contains an integer \(n\), the number of towers.
- The second line contains \(n\) space-separated integers representing the heights of the towers.
- The third line contains an integer \(K\) which is the value used to modify the tower heights.
outputFormat
Output a single integer to standard output (stdout) representing the minimum possible difference between the highest and lowest tower heights after modifications.
## sample4
1 5 8 10
2
5