# Minimize the maximum difference between the heights

• Difficulty Level : Hard
• Last Updated : 30 Jun, 2022

Given heights of n towers and a value k. We need to either increase or decrease the height of every tower by k (only once) where k > 0. The task is to minimize the difference between the heights of the longest and the shortest tower after modifications and output this difference.

Examples:

Input  : arr[] = {1, 15, 10}, k = 6
Output :  Maximum difference is 5.
Explanation : We change 1 to 7, 15 to 9 and 10 to 4. Maximum difference is 5 (between 4 and 9). We can’t get a lower difference.

Input : arr[] = {1, 5, 15, 10}
k = 3
Output : Maximum difference is 8 arr[] = {4, 8, 12, 7}

Input : arr[] = {4, 6}
k = 10
Output : Maximum difference is 2 arr[] = {14, 16} OR {-6, -4}

Input : arr[] = {6, 10}
k = 3
Output : Maximum difference is 2 arr[] = {9, 7}

Input : arr[] = {1, 10, 14, 14, 14, 15}
k = 6
Output: Maximum difference is 5 arr[] = {7, 4, 8, 8, 8, 9}

Input : arr[] = {1, 2, 3}
k = 2
Output: Maximum difference is 2 arr[] = {3, 4, 5}

First, we try to sort the array and make each height of the tower maximum. We do this by decreasing the height of all the towers towards the right by k and increasing all the height of the towers towards the left (by k). It is also possible that the tower you are trying to increase the height doesn’t have the maximum height. Therefore we only need to check whether it has the maximum height or not by comparing it with the last element on the right side which is a[n]-k. Since the array is sorted if the tower’s height is greater than the [n]-k then it’s the tallest tower available. Similar reasoning can also be applied to finding the shortest tower.

Note:- We need not consider where a[i]<k because the height of the tower can’t be negative so we have to neglect that case.

Below is the implementation of the above approach:

## C++

 `#include ` `using` `namespace` `std;`   `// User function Template` `int` `getMinDiff(``int` `arr[], ``int` `n, ``int` `k)` `{` `    ``sort(arr, arr + n);` `    ``int` `ans = arr[n - 1] - arr; ``// Maximum possible height difference`   `    ``int` `tempmin, tempmax;` `    ``tempmin = arr;` `    ``tempmax = arr[n - 1];`   `    ``for` `(``int` `i = 1; i < n; i++) {` `        ``if``(arr[i] - k < 0) ``// if on subtracting k we got negative then continue ` `        ``continue``;` `        ``tempmin= min(arr + k,arr[i] - k); ``// Minimum element when we` `                                                ``// add k to whole array` `        ``tempmax = max(arr[i - 1] + k, arr[n - 1] - k); ``// Maximum element when we` `                                                         ``// subtract k from whole array` `        ``ans = min(ans, tempmax - tempmin); ` `    ``}` `    ``return` `ans;` `}`   `// Driver Code Starts` `int` `main()` `{` `    ``int` `k = 6, n = 6;` `    ``int` `arr[n] = { 7, 4, 8, 8, 8, 9 };` `    ``int` `ans = getMinDiff(arr, n, k);` `    ``cout << ans;` `}`

## Java

 `/*package whatever //do not write package name here */`   `import` `java.io.*;` `import` `java.util.*;`   `// Driver code` `public` `class` `Main {`   `    ``public` `static` `void` `main(String[] args)` `    ``{` `        ``int``[] arr = { ``7``, ``4``, ``8``, ``8``, ``8``, ``9` `};` `        ``int` `k = ``6``;` `        ``int` `ans = getMinDiff(arr, arr.length, k);` `        ``System.out.println(ans);` `    ``}`   `    ``// User function Template for Java` `    ``public` `static` `int` `getMinDiff(``int``[] arr, ``int` `n, ``int` `k)` `    ``{`   `        ``Arrays.sort(arr);` `        ``// Maximum possible height difference` `        ``int` `ans = arr[n - ``1``] - arr[``0``];`   `        ``int` `tempmin, tempmax;` `        ``tempmin = arr[``0``];` `        ``tempmax = arr[n - ``1``];`   `        ``for` `(``int` `i = ``1``; i < n; i++) {`   `            ``// if on subtracting k we got negative then` `            ``// continue` `            ``if` `(arr[i] - k < ``0``)` `                ``continue``;`   `            ``// Minimum element when we add k to whole array` `            ``tempmin = Math.min(arr[``0``] + k, arr[i] - k);`   `            ``// Maximum element when we subtract k from whole` `            ``// array` `            ``tempmax` `                ``= Math.max(arr[i - ``1``] + k, arr[n - ``1``] - k);` `            ``ans = Math.min(ans, tempmax - tempmin);` `        ``}` `        ``return` `ans;` `    ``}` `}`

## Python3

 `# User function Template` `def` `getMinDiff(arr, n, k):` `    ``arr.sort()` `    ``ans ``=` `arr[n ``-` `1``] ``-` `arr[``0``]  ``# Maximum possible height difference`   `    ``tempmin ``=` `arr[``0``]` `    ``tempmax ``=` `arr[n ``-` `1``]`   `    ``for` `i ``in` `range``(``1``, n):` `        ``tempmin ``=` `min``(arr[``0``] ``+` `k, arr[i] ``-` `k)  ` `        `  `        ``# Minimum element when we` `        ``# add k to whole array` `        ``# Maximum element when we` `        ``tempmax ``=` `max``(arr[i ``-` `1``] ``+` `k, arr[n ``-` `1``] ``-` `k)` `        `  `        ``# subtract k from whole array` `        ``ans ``=` `min``(ans, tempmax ``-` `tempmin)`   `    ``return` `ans`   `# Driver Code Starts` `k ``=` `6` `n ``=` `6` `arr ``=` `[``7``, ``4``, ``8``, ``8``, ``8``, ``9``]` `ans ``=` `getMinDiff(arr, n, k)` `print``(ans)`   `# This code is contributed by ninja_hattori.`

## C#

 `using` `System;`   `public` `class` `GFG {`   `  ``static` `public` `int` `getMinDiff(``int``[] arr, ``int` `n, ``int` `k)` `  ``{`   `    ``Array.Sort(arr);` `    ``int` `ans` `      ``= (arr[n - 1] + k)` `      ``- (arr` `         ``+ k); ``// Maximum possible height difference`   `    ``int` `tempmax` `      ``= arr[n - 1] - k; ``// Maximum element when we` `    ``// subtract k from whole array` `    ``int` `tempmin = arr + k; ``// Minimum element when we` `    ``// add k to whole array` `    ``int` `max, min;`   `    ``for` `(``int` `i = 0; i < n - 1; i++) {` `      ``if` `(tempmax > (arr[i] + k)) {` `        ``max = tempmax;` `      ``}` `      ``else` `{` `        ``max = arr[i] + k;` `      ``}`   `      ``if` `(tempmin < (arr[i + 1] - k)) {` `        ``min = tempmin;` `      ``}` `      ``else` `{` `        ``min = arr[i + 1] - k;` `      ``}`   `      ``if` `(ans > (max - min)) {` `        ``ans = max - min;` `      ``}` `    ``}` `    ``return` `ans;` `  ``}`   `  ``static` `public` `void` `Main()` `  ``{` `    ``int``[] arr = { 7, 4, 8, 8, 8, 9 };` `    ``int` `k = 6;` `    ``int` `ans = getMinDiff(arr, arr.Length, k);` `    ``Console.Write(ans);` `  ``}` `}`   `// This code is contributed by ninja_hattori.`

## Javascript

 ``

Output

`5`

Time Complexity: O(nlogn)
Auxiliary Space: O(n)

My Personal Notes arrow_drop_up
Recommended Articles
Page :