# Minimize the maximum difference between the heights

• Difficulty Level : Hard
• Last Updated : 02 Sep, 2021

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} ```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

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 ``using` `namespace` `std;` `#define pii pair``class` `Solution {` `public``:``    ``int` `getMinDiff(``int` `arr[], ``int` `n, ``int` `k)``    ``{` `        ``if` `(n == 1)``            ``return` `0;` `        ``sort(arr, arr + n);` `        ``vector t;` `        ``map<``int``, ``int``> m;` `        ``int` `n_ = 1;` `        ``t.push_back(pii(arr + k, arr));` `        ``t.push_back(pii(arr - k, arr));` `        ``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.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`

My Personal Notes arrow_drop_up