# 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:
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:
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:

1. Find the maximum difference between any two adjacent element in the array and store it in a variable, say worst.
2. Search from best(1 initially) to worst and for every mid value find the number of insertions required.
3. 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.
4. The final worst value after termination of the loop gives the answer.

Below code is the implementation of the above approach:

 `// C++ Program to find the minimum of maximum` `// differerence between adjacent elements` `// after at most K insertions`   `#include ` `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);` `    ``int` `k = 7;`   `    ``cout << minMaxDiff(arr, n, k);` `    ``return` `0;` `}`

 `// Java program to find the minimum ` `// of maximum difference between ` `// adjacent elements after at most` `// K insertions` `import` `java.util.*;`   `class` `GFG{` `    `  `static` `int` `minMaxDiff(``int` `arr[], ``int` `n, ``int` `k)` `{` `    ``int` `max_adj_dif = Integer.MIN_VALUE;` `    `  `    ``// Calculate the maximum` `    ``// adjacent difference` `    ``for``(``int` `i = ``0``; i < n - ``1``; i++)` `        ``max_adj_dif = Math.max(max_adj_dif,` `                      ``Math.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 += (Math.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` `public` `static` `void` `main(String[] args)` `{` `    ``int` `arr[] = { ``3``, ``12``, ``25``, ``50` `};` `    ``int` `n = arr.length;` `    ``int` `k = ``7``;`   `    ``System.out.println(minMaxDiff(arr, n, k));` `}` `}`   `// This code is contributed by ANKITKUMAR34`

 `# Python3 program to find the minimum ` `# of maximum difference between ` `# adjacent elements after at most` `# K insertions` `def` `minMaxDiff(arr, n, k):`   `    ``max_adj_dif ``=` `float``(``'-inf'``);` `    `  `    ``# Calculate the maximum` `    ``# adjacent difference` `    ``for` `i ``in` `range``(n ``-` `1``):` `        ``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` `    ``best ``=` `1``;` `    ``worst ``=` `max_adj_dif;` `    `  `    ``while` `(best < worst):` `        ``mid ``=` `(best ``+` `worst) ``/``/` `2``;`   `        ``# To store the no of insertions` `        ``# required for respective` `        ``# values of mid` `        ``required ``=` `0`   `        ``for` `i ``in` `range``(n ``-` `1``):` `            ``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` `arr ``=` `[ ``3``, ``12``, ``25``, ``50` `]` `n ``=` `len``(arr)` `k ``=` `7`   `print``(minMaxDiff(arr, n, k))`   `# This code is contributed by ANKITKUMAR34`

 `// C# program to find the minimum ` `// of maximum difference between ` `// adjacent elements after at most` `// K insertions` `using` `System;` `class` `GFG{` `    `  `static` `int` `minMaxDiff(``int` `[]arr, ``int` `n, ``int` `k)` `{` `    ``int` `max_adj_dif = ``int``.MinValue;` `    `  `    ``// Calculate the maximum` `    ``// adjacent difference` `    ``for``(``int` `i = 0; i < n - 1; i++)` `        ``max_adj_dif = Math.Max(max_adj_dif,` `                      ``Math.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 += (Math.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` `public` `static` `void` `Main(String[] args)` `{` `    ``int` `[]arr = { 3, 12, 25, 50 };` `    ``int` `n = arr.Length;` `    ``int` `k = 7;`   `    ``Console.WriteLine(minMaxDiff(arr, n, k));` `}` `}`   `// This code is contributed by Princi Singh`

Output:
```5

```

Check out this Author's contributed articles.

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.

Improved By : ANKITKUMAR34, princi singh

Article Tags :