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:

  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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Output:

5

competitive-programming-img




My Personal Notes arrow_drop_up

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.