Skip to content
Related Articles

Related Articles

Improve Article

Minimize maximum difference between any pair of array elements by exactly one removal

  • Difficulty Level : Basic
  • Last Updated : 24 Jun, 2021

Given an array arr[] consisting of N integers(N > 2), the task is to minimize the maximum difference between any pair of elements (arr[i], arr[j]) by removing exactly one element.

Examples:

Input: arr[] = {1, 3, 4, 7}
Output: 3
Explanation:
Removing the element 7 from array, modifies the array  to {1, 3, 4}.
Here (4, 1) has difference = 4 – 1 = 3 which is minimum possible maximum difference. 

Input: arr[] = {1, 2, 3, 4}
Output: 2

Naive Approach: The simplest approach to solve the given problem is to remove every element one by one and check which element gives the minimized maximum difference between every pair of elements.



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

Efficient Approach: The above approach can also be optimized by observing the fact that the maximum difference is equal to the difference between the maximum and the minimum element of the given array. So, removing the maximum element or removing the minimum element always gives the optimal answer.

Therefore, the idea is to sort the given array in ascending order and print the minimum of the value (arr[N – 2] – arr[0]) and (arr[N – 1] – arr[1]) as the resultant minimized maximum difference.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the minimum maximum
// difference after removing one element
int findMinDifference(int arr[], int n)
{
    // Stores the resultant minimized
    // maximum difference
    int ans = 0;
 
    // Sort the given array
    sort(arr, arr + n);
 
    // Find the minimum maximum
    // difference
    ans = min(arr[n - 2] - arr[0],
              arr[n - 1] - arr[1]);
 
    // Return the result
    return ans;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 3, 4, 7 };
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << findMinDifference(arr, N);
 
    return 0;
}

Java




// Java program for the above approach
 
import java.io.*;
import java.util.*;
 
class GFG {
 
  // Function to find the minimum maximum
  // difference after removing one element
  static int findMinDifference(int []arr, int n)
  {
 
    // Stores the resultant minimized
    // maximum difference
    int ans = 0;
 
    // Sort the given array
    Arrays.sort(arr);
 
    // Find the minimum maximum
    // difference
    ans = Math.min(arr[n - 2] - arr[0],
                   arr[n - 1] - arr[1]);
 
    // Return the result
    return ans;
  }
 
  // Driver Code
 
  public static void main (String[] args) {
 
    int []arr = { 1, 3, 4, 7 };
    int N = arr.length;
 
 
    System.out.println(findMinDifference(arr, N));
  }
 
}
 
// This code is contributed by unknown2108

Python3




# Python3 program for the above approach
 
# Function to find the minimum maximum
# difference after removing one element
def findMinDifference(arr, n):
     
    # Stores the resultant minimized
    # maximum difference
    ans = 0
 
    # Sort the given array
    arr = sorted(arr)
 
    # Find the minimum maximum
    # difference
    ans = min(arr[n - 2] - arr[0],
              arr[n - 1] - arr[1])
 
    # Return the result
    return ans
 
# Driver Code
if __name__ == '__main__':
     
    arr = [ 1, 3, 4, 7 ]
    N = len(arr)
     
    print (findMinDifference(arr, N))
 
# This code is contributed by mohit kumar 29

C#




// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to find the minimum maximum
// difference after removing one element
static int findMinDifference(int []arr, int n)
{
     
    // Stores the resultant minimized
    // maximum difference
    int ans = 0;
 
    // Sort the given array
    Array.Sort(arr);
 
    // Find the minimum maximum
    // difference
    ans = Math.Min(arr[n - 2] - arr[0],
                   arr[n - 1] - arr[1]);
 
    // Return the result
    return ans;
}
 
// Driver Code
public static void Main()
{
    int []arr = { 1, 3, 4, 7 };
    int N = arr.Length;
     
    Console.Write(findMinDifference(arr, N));
}
}
 
// This code is contributed by ipg2016107

Javascript




<script>
// Javascript program for the above approach
 
// Function to find the minimum maximum
// difference after removing one element
function findMinDifference(arr, n)
{
 
    // Stores the resultant minimized
    // maximum difference
    let ans = 0;
  
    // Sort the given array
    arr.sort(function(a,b){return a-b;});
  
    // Find the minimum maximum
    // difference
    ans = Math.min(arr[n - 2] - arr[0],
                   arr[n - 1] - arr[1]);
  
    // Return the result
    return ans;
}
 
// Driver Code
let arr=[1, 3, 4, 7];
let N = arr.length;
document.write(findMinDifference(arr, N));
 
// This code is contributed by patel2127
</script>
Output: 
3

 

Time Complexity: O(N*log N)
Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :