# Minimize the maximum difference between the heights

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 = 6Output :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 = 3Output :Maximum difference is 8 arr[] = {4, 8, 12, 7}Input :arr[] = {4, 6} k = 10Output :Maximum difference is 2 arr[] = {14, 16} OR {-6, -4}Input :arr[] = {6, 10} k = 3Output :Maximum difference is 2 arr[] = {9, 7}Input :arr[] = {1, 10, 14, 14, 14, 15} k = 6Output:Maximum difference is 5 arr[] = {7, 4, 8, 8, 8, 9}Input :arr[] = {1, 2, 3} k = 2Output:Maximum difference is 2 arr[] = {3, 4, 5}

Source: Adobe Interview Experience | Set 24 (On-Campus for MTS)

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 towards the right side which is a[n]-k. Since the array is sorted if the tower’s height is greater than the a[n]-k then it’s the tallest tower available. Similar reasoning can also be applied for 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.

**Time Complexity:** O(nlogn)

**Space Complexity:** O(n)

## C++

`#include <bits/stdc++.h>` `using` `namespace` `std;` `#define pii pair<int, int>` `class` `Solution {` `public` `:` ` ` `int` `getMinDiff(` `int` `arr[], ` `int` `n, ` `int` `k)` ` ` `{` ` ` `if` `(n == 1)` ` ` `return` `0;` ` ` `sort(arr, arr + n);` ` ` `vector<pii> t;` ` ` `map<` `int` `, ` `int` `> m;` ` ` `int` `n_ = 1;` ` ` `t.push_back(pii(arr[0] + k, arr[0]));` ` ` `t.push_back(pii(arr[0] - k, arr[0]));` ` ` `for` `(` `int` `i = 1; i < n; i++) {` ` ` `if` `(arr[i] != arr[i - 1]) {` ` ` `t.push_back(pii(arr[i] + k, arr[i]));` ` ` `t.push_back(pii(arr[i] - k, arr[i]));` ` ` `m[arr[i]] = 0;` ` ` `n_++;` ` ` `}` ` ` `}` ` ` `sort(t.begin(), t.end());` ` ` `int` `l = 0, r = 0;` ` ` `int` `ans = t[t.size() - 1].first - t[0].first;` ` ` `int` `count = 0;` ` ` `while` `(r < t.size()) {` ` ` `while` `(r < t.size() and count < n_) {` ` ` `if` `(m[t[r].second] == 0)` ` ` `count++;` ` ` `m[t[r].second]++;` ` ` `r++;` ` ` `}` ` ` `if` `(r == t.size() and count < n_)` ` ` `break` `;` ` ` `ans = min(ans, t[r - 1].first - t[l].first);` ` ` `while` `(l <= r and count >= n_) {` ` ` `if` `(m[t[l].second] == 1)` ` ` `count--;` ` ` `m[t[l].second]--;` ` ` `ans = min(ans, t[r - 1].first - t[l].first);` ` ` `l++;` ` ` `}` ` ` `}` ` ` `return` `ans;` ` ` `}` `};` `int` `main()` `{` ` ` `Solution s;` ` ` `int` `k = 5, n = 10;` ` ` `int` `arr[n] = { 8, 1, 5, 4, 7, 5, 7, 9, 4, 6 };` ` ` `cout << s.getMinDiff(arr, n, k);` `}` |

**Output**

7