# Minimize the maximum difference of adjacent elements after at most K insertions

Given an array of **N** elements, the task is to minimize the maximum difference of adjacent elements by inserting at most **K** elements in the array.

**Examples:**

Input:arr = [2, 6, 8] K = 1

Output:2

Explanation:

After insertion of 4 in between 2 and 6, the array becomes [2, 4, 6, 8]. In this case the maximum difference between any adjacent element is 2, which is the minimum that can be.

Input:arr = [3, 12] K = 2

Output:3

Explanation:

After insertion of 6 and 9 in between 3 and 12, the array becomes [3, 6, 9, 12]. In this case the maximum difference between any adjacent element is 3, which is the minimum that can be.

**Approach:** In order to solve this problem, we are using the following Binary Search based approach:

- Find the maximum difference between any two adjacent element in the array and store it in a variable, say
**worst**. - Search from
**best**(1 initially) to**worst**and for every**mid**value find the number of insertions required. - Whenever the number of insertions is greater than
**K**for a particular value of**mid**, search between**[mid + 1, worst]**, that is the higher half. Otherwise search between**[best, mid-1]**, that is the lower half to check if the maximum difference can be further minimized with at most K insertions. - The final
**worst**value after termination of the loop gives the answer.

Below code is the implementation of the above approach:

## C++

`// C++ Program to find the minimum of maximum ` `// differerence between adjacent elements ` `// after at most K insertions ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `int` `minMaxDiff(` `int` `arr[], ` `int` `n, ` `int` `k) ` `{ ` ` ` `int` `max_adj_dif = INT_MIN; ` ` ` `// Calculate the maximum ` ` ` `// adjacent difference ` ` ` `for` `(` `int` `i = 0; i < n - 1; i++) ` ` ` `max_adj_dif ` ` ` `= max(max_adj_dif, ` ` ` `abs` `(arr[i] - arr[i + 1])); ` ` ` ` ` `// If the maximum adjacent ` ` ` `// difference is already zero ` ` ` `if` `(max_adj_dif == 0) ` ` ` `return` `0; ` ` ` ` ` `// best and worst specifies ` ` ` `// range of the maximum ` ` ` `// adjacent difference ` ` ` `int` `best = 1; ` ` ` `int` `worst = max_adj_dif; ` ` ` `int` `mid, required; ` ` ` ` ` `while` `(best < worst) { ` ` ` ` ` `mid = (best + worst) / 2; ` ` ` ` ` `// To store the no of insertions ` ` ` `// required for respective ` ` ` `// values of mid ` ` ` `required = 0; ` ` ` ` ` `for` `(` `int` `i = 0; i < n - 1; i++) { ` ` ` ` ` `required += (` `abs` `(arr[i] ` ` ` `- arr[i + 1]) ` ` ` `- 1) ` ` ` `/ mid; ` ` ` `} ` ` ` ` ` `// If the number of insertions ` ` ` `// required exceeds K ` ` ` `if` `(required > k) ` ` ` `best = mid + 1; ` ` ` ` ` `// Otherwise ` ` ` `else` ` ` `worst = mid; ` ` ` `} ` ` ` ` ` `return` `worst; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 3, 12, 25, 50 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` `int` `k = 7; ` ` ` ` ` `cout << minMaxDiff(arr, n, k); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

5

## Recommended Posts:

- Minimize the maximum difference between adjacent elements in an array
- Minimize the maximum absolute difference of adjacent elements in a circular array
- Minimize the difference between minimum and maximum elements
- Maximum sum of difference of adjacent elements
- Maximum length subarray with difference between adjacent elements as either 0 or 1
- Maximum subsequence sum with adjacent elements having atleast K difference in index
- Missing occurrences of a number in an array such that maximum absolute difference of adjacent elements is minimum
- Minimize sum of adjacent difference with removal of one element from array
- Minimize the maximum difference between the heights
- Minimize the maximum minimum difference after one removal from array
- Minimize the difference between the maximum and minimum values of the modified array
- Sort elements of an array in increasing order of absolute difference of adjacent elements
- Maximum sum such that no two elements are adjacent
- Maximum sum such that no two elements are adjacent | Set 2
- Maximum set bit sum in array without considering adjacent elements

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.