Given an array of integers and an integer . It is allowed to modify an element either by increasing or decreasing them by k (only once).

The **task is to minimize** and print the maximum difference between the shortest and longest towers.

**Examples:**

Input: arr[] = {1, 10, 8, 5}, k = 2Output: Max height difference = 5Explanation:modified arr[]={3, 8, 6, 7} max difference: 5Input: arr[] = {3, 16, 12, 9, 20}, k = 3Output: Max height difference: 11

**Approach:**

- Find the
**max**and**min**element present in the array. - Check whether the difference between the
**max**and**min**element is less than or equal to**k**or not:- If yes, then return the difference between the max and min element.
- otherwise go to step 3.

- Calculate the average of the max and min element of the array.
- Traverse the array and do the following operations:
- If the array element is greater than the average then decrease it by k.
- If the array element is smaller than the average then increase it by k.

- Return the difference between the max and min element of the modified array.

Below is the implementation of above approach:

## C++

`// C++ program to minimize the difference between ` `// minimum and maximum elements ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to minimize the difference between ` `// minimum and maximum elements ` `int` `minimizeDiff(` `int` `* arr, ` `int` `n, ` `int` `k) ` `{ ` ` ` `// Find max and min elements of the array ` ` ` `int` `max = *(max_element(arr, arr + n)); ` ` ` `int` `min = *(min_element(arr, arr + n)); ` ` ` ` ` `// Check whether the difference between ` ` ` `// the max and min element is less than ` ` ` `// or equal to k or not ` ` ` `if` `((max - min) <= k) { ` ` ` `return` `(max - min); ` ` ` `} ` ` ` ` ` `// Calculate average of max and min ` ` ` `int` `avg = (max + min) / 2; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `// If the array element is greater than the ` ` ` `// average then decrease it by k ` ` ` `if` `(arr[i] > avg) ` ` ` `arr[i] -= k; ` ` ` `// If the array element is smaller than the ` ` ` `// average then increase it by k ` ` ` `else` ` ` `arr[i] += k; ` ` ` `} ` ` ` ` ` `// Find max and min of the modified array ` ` ` `max = *(max_element(arr, arr + n)); ` ` ` `min = *(min_element(arr, arr + n)); ` ` ` ` ` `// return the new difference ` ` ` `return` `(max - min); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 3, 16, 12, 9, 20 }; ` ` ` `int` `n = 5; ` ` ` `int` `k = 3; ` ` ` ` ` `cout << ` `"Max height difference = "` ` ` `<< minimizeDiff(arr, n, k) << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 program to minimize the ` `# difference between minimum and ` `# maximum elements ` ` ` `# Function to minimize the difference ` `# between minimum and maximum elements ` `def` `minimizeDiff(arr, n, k) : ` ` ` ` ` `# Find max and min elements ` ` ` `# of the array ` ` ` `max_element ` `=` `max` `(arr) ` ` ` `min_element ` `=` `min` `(arr) ` ` ` ` ` `# Check whether the difference between ` ` ` `# the max and min element is less than ` ` ` `# or equal to k or not ` ` ` `if` `((max_element ` `-` `min_element) <` `=` `k) : ` ` ` `return` `(max_element ` `-` `min_element) ` ` ` ` ` `# Calculate average of max and min ` ` ` `avg ` `=` `(max_element ` `+` `min_element) ` `/` `/` `2` ` ` ` ` `for` `i ` `in` `range` `(n): ` ` ` ` ` `# If the array element is greater than ` ` ` `# the average then decrease it by k ` ` ` `if` `(arr[i] > avg) : ` ` ` `arr[i] ` `-` `=` `k ` ` ` ` ` `# If the array element is smaller than ` ` ` `# the average then increase it by k ` ` ` `else` `: ` ` ` `arr[i] ` `+` `=` `k ` ` ` ` ` `# Find max and min of the ` ` ` `# modified array ` ` ` `max_element ` `=` `max` `(arr) ` ` ` `min_element ` `=` `min` `(arr) ` ` ` ` ` `# return the new difference ` ` ` `return` `(max_element ` `-` `min_element); ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `arr ` `=` `[ ` `3` `, ` `16` `, ` `12` `, ` `9` `, ` `20` `] ` ` ` `n ` `=` `5` ` ` `k ` `=` `3` ` ` ` ` `print` `(` `"Max height difference ="` `, ` ` ` `minimizeDiff(arr, n, k)) ` ` ` `# This code is contributed by Ryuga ` |

*chevron_right*

*filter_none*

**Output:**

Max height difference = 11

**Time Complexity**: O( N )

## Recommended Posts:

- Minimize the difference between the maximum and minimum values of the modified array
- Choose k array elements such that difference of maximum and minimum is minimized
- Minimize the maximum difference between the heights
- Find minimum difference between any two elements
- Maximum difference between two subsets of m elements
- Sum of minimum difference between consecutive elements of an array
- Maximum difference between group of k-elements and rest of the array.
- Minimum absolute difference of adjacent elements in a circular array
- Find maximum difference between nearest left and right smaller elements
- Find maximum number of elements such that their absolute difference is less than or equal to 1
- Maximum difference between two elements such that larger element appears after the smaller number
- Sum of minimum and maximum elements of all subarrays of size k.
- Rearrange a given list such that it consists of alternating minimum maximum elements
- Recursive Programs to find Minimum and Maximum elements of array
- Find minimum and maximum elements in singly Circular Linked List

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.