Open In App

Minimize maximum difference between adjacent elements possible by removing a single array element

Last Updated : 05 May, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Given an sorted array arr[] consisting of N elements, the task is to find the minimum of all maximum differences between adjacent elements of all arrays obtained by removal of any single array element.

Examples:

Input: arr[ ] = { 1, 3, 7, 8}
Output: 5
Explanation:
All possible arrays after removing a single element are as follows:
{3, 7, 8}: Difference between adjacent elements are { 4, 1}. Maximum = 4.
{ 1, 7, 8}: Difference between adjacent elements are { 6, 1}. Maximum = 6.
{ 1, 3, 8}: Difference between adjacent elements are { 2, 5}. Maximum = 5.
Finally, minimum of (4, 6, 5) is 4, which is the required output.

Input: arr[ ] = { 1, 2, 3, 4, 5}
Output: 1
Explanation:
All possible arrays after removing a single element are as follows:
{ 2, 3, 4, 5}: Difference between adjacent elements are { 1, 1, 1}. Maximum = 1.
{ 1, 3, 4, 5}: Difference between adjacent elements are { 2, 1, 1}. Maximum = 2.
{ 1, 2, 4, 5}: Difference between adjacent elements are { 1, 2, 1}. Maximum = 2.
{ 1, 2, 3, 5}: Difference between adjacent elements are { 1, 1, 2}. Maximum = 2.
Finally, minimum of (1, 2, 2, 2) is 1, which is the required output.

Approach: Follow the steps to solve the problem

  • Declare a variable MinValue = INT_MAX to store the final answer.
  • Traverse the array, for i in range [0, N – 1]
    • Declare a vector new_arr which is a copy of arr[] except element arr[i]
    • Store the maximum adjacent difference of new_arr in a variable diff
    • Update MinValue = min(MinValue, diff)
  • Return MinValue as the final answer.

Below is the implementation of above approach.

C++




// C++ Program to implement
// the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find maximum difference
// between adjacent array elements
int maxAdjacentDifference(vector<int> A)
{
    // Store the maximum difference
    int diff = 0;
 
// Traverse the array
    for (int i = 1; i < (int)A.size(); i++) {
 
        // Update maximum difference
        diff = max(diff, A[i] - A[i - 1]);
    }
 
    return diff;
}
 
// Function to calculate the minimum
// of maximum difference between
// adjacent array elements possible
// by removing a single array element
int MinimumValue(int arr[], int N)
{
    // Stores the required minimum
    int MinValue = INT_MAX;
 
    for (int i = 0; i < N; i++) {
 
        // Stores the updated array
        vector<int> new_arr;
 
        for (int j = 0; j < N; j++) {
 
            // Skip the i-th element
            if (i == j)
                continue;
 
            new_arr.push_back(arr[j]);
        }
 
        // Update MinValue
        MinValue
            = min(MinValue,
                  maxAdjacentDifference(new_arr));
    }
 
    // return MinValue
    return MinValue;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 3, 7, 8 };
    int N = sizeof(arr) / sizeof(int);
    cout << MinimumValue(arr, N);
 
    return 0;
}


Java




// Java program for the above approach
import java.util.*;
 
class GFG
{
 
// Function to find maximum difference
// between adjacent array elements
static int maxAdjacentDifference(ArrayList<Integer> A)
{
   
    // Store the maximum difference
    int diff = 0;
 
// Traverse the array
    for (int i = 1; i < (int)A.size(); i++)
    {
 
        // Update maximum difference
        diff = Math.max(diff, A.get(i) - A.get(i - 1));
    }
 
    return diff;
}
 
// Function to calculate the minimum
// of maximum difference between
// adjacent array elements possible
// by removing a single array element
static int MinimumValue(int arr[], int N)
{
   
    // Stores the required minimum
    int MinValue = Integer.MAX_VALUE;
 
    for (int i = 0; i < N; i++) {
 
        // Stores the updated array
        ArrayList<Integer> new_arr=new ArrayList<>();
 
        for (int j = 0; j < N; j++) {
 
            // Skip the i-th element
            if (i == j)
                continue;
 
            new_arr.add(arr[j]);
        }
 
        // Update MinValue
        MinValue
            = Math.min(MinValue,
                  maxAdjacentDifference(new_arr));
    }
 
    // return MinValue
    return MinValue;
 
}
 
  // Driver code
    public static void main (String[] args) {
     int arr[] = { 1, 3, 7, 8 };
    int N = arr.length;
   System.out.print(MinimumValue(arr, N));
 
    }
}
 
// This code is contributed by offbeat


Python3




# Python3 program to implement
# the above approach
import sys
 
# Function to find maximum difference
# between adjacent array elements
def maxAdjacentDifference(A):
     
    # Store the maximum difference
    diff = 0
 
    # Traverse the array
    for i in range(1, len(A), 1):
         
        # Update maximum difference
        diff = max(diff, A[i] - A[i - 1])
 
    return diff
 
# Function to calculate the minimum
# of maximum difference between
# adjacent array elements possible
# by removing a single array element
def MinimumValue(arr, N):
     
    # Stores the required minimum
    MinValue = sys.maxsize
 
    for i in range(N):
         
        # Stores the updated array
        new_arr = []
 
        for j in range(N):
             
            # Skip the i-th element
            if (i == j):
                continue
 
            new_arr.append(arr[j])
 
        # Update MinValue
        MinValue = min(MinValue,
                       maxAdjacentDifference(new_arr))
 
    # return MinValue
    return MinValue
 
# Driver Code
if __name__ == '__main__':
     
    arr =  [ 1, 3, 7, 8 ]
    N = len(arr)
     
    print(MinimumValue(arr, N))
 
# This code is contributed by SURENDRA_GANGWAR


C#




// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to find maximum difference
// between adjacent array elements
static int maxAdjacentDifference(List<int> A)
{
     
    // Store the maximum difference
    int diff = 0;
 
    // Traverse the array
    for(int i = 1; i < A.Count; i++)
    {
         
        // Update maximum difference
        diff = Math.Max(diff, A[i] - A[i - 1]);
    }
    return diff;
}
 
// Function to calculate the minimum
// of maximum difference between
// adjacent array elements possible
// by removing a single array element
static int MinimumValue(int[] arr, int N)
{
     
    // Stores the required minimum
    int MinValue = Int32.MaxValue;
 
    for(int i = 0; i < N; i++)
    {
         
        // Stores the updated array
        List<int> new_arr = new List<int>();
 
        for(int j = 0; j < N; j++)
        {
             
            // Skip the i-th element
            if (i == j)
                continue;
 
            new_arr.Add(arr[j]);
        }
 
        // Update MinValue
        MinValue = Math.Min(MinValue,
             maxAdjacentDifference(new_arr));
    }
 
    // Return MinValue
    return MinValue;
}
 
// Driver Code
public static void Main(string[] args)
{
    int[] arr = { 1, 3, 7, 8 };
    int N = arr.Length;
     
    Console.WriteLine(MinimumValue(arr, N));
}
}
 
// This code is contributed by ukasp


Javascript




<script>
 
// JavaScript program to implement
// the above approach
 
// Function to find maximum difference
// between adjacent array elements
function maxAdjacentDifference(A)
{
    
    // Store the maximum difference
    let diff = 0;
  
// Traverse the array
    for (let i = 1; i < A.length; i++)
    {
  
        // Update maximum difference
        diff = Math.max(diff, A[i] - A[i-1]);
    }
  
    return diff;
}
  
// Function to calculate the minimum
// of maximum difference between
// adjacent array elements possible
// by removing a single array element
function MinimumValue(arr, N)
{
    
    // Stores the required minimum
    let MinValue = Number.MAX_VALUE;
  
    for (let i = 0; i < N; i++) {
  
        // Stores the updated array
        let new_arr=[];
  
        for (let j = 0; j < N; j++) {
  
            // Skip the i-th element
            if (i == j)
                continue;
  
            new_arr.push(arr[j]);
        }
  
        // Update MinValue
        MinValue
            = Math.min(MinValue,
                  maxAdjacentDifference(new_arr));
    }
  
    // return MinValue
    return MinValue;
  
}
 
// Driver code
 
    let arr = [ 1, 3, 7, 8 ];
    let N = arr.length;
   document.write(MinimumValue(arr, N));
            
</script>


Output: 

4

 

Time Complexity : O(N2)
Auxiliary Space: O(N)

 



Similar Reads

Minimize difference between maximum and minimum array elements by removing a K-length subarray
Given an array arr[] consisting of N integers and an integer K, the task is to find the minimum difference between the maximum and minimum element present in the array after removing any subarray of size K. Examples: Input: arr[] = {4, 5, 8, 9, 1, 2}, K = 2Output: 4Explanation: Remove the subarray {8, 9}. The minimum difference between maximum and
10 min read
Reduce an array to a single element by repeatedly removing larger element from a pair with absolute difference at most K
Given an array arr[] consisting of N integers and a positive integer K, the task is to check if the given array can be reduced to a single element by repeatedly removing the larger of the two elements present in a pair whose absolute difference is at most K. If the array can be reduced to a single element, then print "Yes". Otherwise, print "No". E
11 min read
Maximum possible difference between two Subarrays after removing N elements from Array
Given an array arr[] which is of 3*N size, the task is to remove N elements and divide the whole array into two equal parts such that the difference of the sum of the left subarray and right subarray should yield to maximum. Examples: Input: arr[] = [5, 4, 4, 2, 3, 3]Output: 4Explanation: The '2' elements to be removed are [4, 3]. and when you divi
10 min read
Minimize the maximum difference between adjacent elements in an array
Given a non-decreasing array arr[] and an integer K, the task is to remove K elements from the array such that maximum difference between adjacent element is minimum.Note: K &lt; N - 2 Examples: Input: arr[] = {3, 7, 8, 10, 14}, K = 2 Output: 2 Explanation: After removing elements A[0] and A[4], The maximum difference between adjacent elements is m
24 min read
Maximum score possible by removing substrings made up of single distinct character
Given a binary string S and an array A[], both of size N, the task is to find the maximum score possible by removing substrings of any length, say K, consisting of the same characters, and adding A[K] to the score. Examples: Input: S = "abb", A = [1, 3, 1]Output: 4Explanation: Initially, score = 0 and S="abb" Remove the substring {S[1], .. S[2]}, o
14 min read
Reduce array to a single element by repeatedly removing an element from any increasing pair
Given an array arr[] consisting of permutation in the range [1, N], the task is to check if the given array can be reduced to a single non-zero element by performing the following operations: Select indices i and j such that i &lt; j and arr[i] &lt; arr[j] and convert one of the two elements to 0. If it is possible to reduce the array to a single e
9 min read
Minimize sum of given array by removing all occurrences of a single digit
Given an array arr[] of size N, the task is to minimize the sum by removing all the occurrences of a single digit. Examples: Input: arr[] = {34, 23, 85, 93}Output: 100Explanation: Removing the occurrences of the digit 3 from each element of the array modifies arr[] to {4, 2, 85, 9}. Therefore, minimized sum of the array = 4 + 2 + 85 + 9 = 100. Inpu
6 min read
Minimize the maximum absolute difference of adjacent elements in a circular array
Given a circular array arr of N integers, the task is to minimize the maximum absolute difference of adjacent elements of the array without any removals. Examples: Input: arr[] = {1, 3, 10, 2, 0, 9, 6} Output: {0, 2, 6, 10, 9, 3, 1} Explanation: In the above example, the maximum difference between adjacent elements is 6, which is between 9 and 3. O
6 min read
Minimize difference between maximum and minimum element of all possible subarrays
Given an array arr[ ] of size N, the task is to find the minimum difference between maximum and minimum elements of all possible sized subarrays of arr[ ]. Examples: Input: arr[] = { 5, 14, 7, 10 } Output: 3Explanation: {7, 10} is the subarray having max element = 10 &amp; min element = 7, and their difference = 10 - 7 = 3 Input: arr[] = { 2, 6, 15
5 min read
Reduce array to a single element by repeatedly replacing adjacent unequal pairs with their maximum
Given an array arr[] consisting of N integers, the task is to reduce the given array to a single element by repeatedly replacing any pair of consecutive unequal elements, say arr[i] and arr[i+1] with max(arr[i], arr[i + 1]) + 1. If possible, print the index of the element from where the operation can be started. Otherwise, print -1. Examples: Input
15 min read
Practice Tags :