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.
Input: arr = [2, 6, 8] K = 1
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
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:
- Minimize insertions in an array to obtain all sums upto N
- Minimize the maximum difference between adjacent elements in an array
- Minimize the maximum absolute difference of adjacent elements in a circular array
- Minimize difference between maximum and minimum of Array by at most K replacements
- Maximum subsequence sum of at most K-distant adjacent elements
- Minimum array insertions required to make consecutive difference <= K
- Minimize the sum of differences of consecutive elements after removing exactly K elements
- Minimize the maximum minimum difference after one removal from array
- Minimize difference after changing all odd elements to even
- Maximum Subarray Sum after inverting at most two elements
- Minimize the cost to make all the adjacent elements distinct in an Array
- Minimize array length by repeatedly replacing pairs of unequal adjacent array elements by their sum
- Minimum insertions to make a Co-prime array
- Minimum insertions to make XOR of an Array equal to half of its sum
- Minimize sum of adjacent difference with removal of one element from array
- Nth positive number whose absolute difference of adjacent digits is at most 1
- Count of numbers upto N having absolute difference of at most K between any two adjacent digits
- Minimize the sum after choosing elements from the given three arrays
- Minimize the difference between minimum and maximum elements
- Maximum possible sub-array sum after at most X swaps
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.