Minimize maximum difference between any pair of array elements by exactly one removal
Last Updated :
16 Feb, 2023
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++
#include <bits/stdc++.h>
using namespace std;
int findMinDifference( int arr[], int n)
{
int ans = 0;
sort(arr, arr + n);
ans = min(arr[n - 2] - arr[0],
arr[n - 1] - arr[1]);
return ans;
}
int main()
{
int arr[] = { 1, 3, 4, 7 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << findMinDifference(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static int findMinDifference( int []arr, int n)
{
int ans = 0 ;
Arrays.sort(arr);
ans = Math.min(arr[n - 2 ] - arr[ 0 ],
arr[n - 1 ] - arr[ 1 ]);
return ans;
}
public static void main (String[] args) {
int []arr = { 1 , 3 , 4 , 7 };
int N = arr.length;
System.out.println(findMinDifference(arr, N));
}
}
|
Python3
def findMinDifference(arr, n):
ans = 0
arr = sorted (arr)
ans = min (arr[n - 2 ] - arr[ 0 ],
arr[n - 1 ] - arr[ 1 ])
return ans
if __name__ = = '__main__' :
arr = [ 1 , 3 , 4 , 7 ]
N = len (arr)
print (findMinDifference(arr, N))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int findMinDifference( int []arr, int n)
{
int ans = 0;
Array.Sort(arr);
ans = Math.Min(arr[n - 2] - arr[0],
arr[n - 1] - arr[1]);
return ans;
}
public static void Main()
{
int []arr = { 1, 3, 4, 7 };
int N = arr.Length;
Console.Write(findMinDifference(arr, N));
}
}
|
Javascript
<script>
function findMinDifference(arr, n)
{
let ans = 0;
arr.sort( function (a,b){ return a-b;});
ans = Math.min(arr[n - 2] - arr[0],
arr[n - 1] - arr[1]);
return ans;
}
let arr=[1, 3, 4, 7];
let N = arr.length;
document.write(findMinDifference(arr, N));
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(N)
Efficient Approach:-
- As in the above approach we are either removing the maximum element or the minimum element
- So we can do this work without sorting
- We have to just find out the 2 maximum and 2 minimum elements from the array
- Answer will be minimum of (2nd largest-first minimum) or (first lasgest-second minimum).
Implementation:-
C++
#include <bits/stdc++.h>
using namespace std;
int findMinDifference( int arr[], int n)
{
int firstmin=INT_MAX,secondmin=INT_MAX,firstmax=INT_MIN,secondmax=INT_MIN;
for ( int i=0;i<n;i++)
{
if (arr[i]<firstmin)
{
secondmin=firstmin;
firstmin=arr[i];
}
else if (arr[i]<secondmin)
{
secondmin=arr[i];
}
if (arr[i]>firstmax)
{
secondmax=firstmax;
firstmax=arr[i];
}
else if (arr[i]>secondmax)
{
secondmax=arr[i];
}
}
return min(secondmax-firstmin,firstmax-secondmin);
}
int main()
{
int arr[] = { 1, 3, 4, 7 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << findMinDifference(arr, N);
return 0;
}
|
Java
import java.lang.*;
import java.util.*;
class Main {
static int findMinDifference( int arr[], int n)
{
int firstmin
= Integer.MAX_VALUE,
secondmin = Integer.MAX_VALUE,
firstmax = Integer.MIN_VALUE,
secondmax = Integer.MIN_VALUE;
for ( int i = 0 ; i < n; i++) {
if (arr[i] < firstmin) {
secondmin = firstmin;
firstmin = arr[i];
}
else if (arr[i] < secondmin) {
secondmin = arr[i];
}
if (arr[i] > firstmax) {
secondmax = firstmax;
firstmax = arr[i];
}
else if (arr[i] > secondmax) {
secondmax = arr[i];
}
}
return Math.min(secondmax - firstmin,
firstmax - secondmin);
}
public static void main(String[] args)
{
int arr[] = { 1 , 3 , 4 , 7 };
int N = arr.length;
System.out.println(findMinDifference(arr, N));
}
}
|
Python3
import sys
def findMinDifference(arr, n):
firstmin = sys.maxsize
secondmin = sys.maxsize
firstmax = - sys.maxsize - 1
secondmax = - sys.maxsize - 1
for i in range (n):
if arr[i] < firstmin:
secondmin = firstmin
firstmin = arr[i]
elif arr[i] < secondmin:
secondmin = arr[i]
if arr[i] > firstmax:
secondmax = firstmax
firstmax = arr[i]
elif arr[i] > secondmax:
secondmax = arr[i]
return min (secondmax - firstmin, firstmax - secondmin)
arr = [ 1 , 3 , 4 , 7 ]
N = len (arr)
print (findMinDifference(arr, N))
|
Javascript
const findMinDifference = (arr, n) => {
let firstmin = Number.MAX_SAFE_INTEGER;
let secondmin = Number.MAX_SAFE_INTEGER;
let firstmax = Number.MIN_SAFE_INTEGER;
let secondmax = Number.MIN_SAFE_INTEGER;
for (let i = 0; i < n; i++) {
if (arr[i] < firstmin) {
secondmin = firstmin;
firstmin = arr[i];
} else if (arr[i] < secondmin) {
secondmin = arr[i];
}
if (arr[i] > firstmax) {
secondmax = firstmax;
firstmax = arr[i];
} else if (arr[i] > secondmax) {
secondmax = arr[i];
}
}
return Math.min(secondmax - firstmin, firstmax - secondmin);
}
const arr = [1, 3, 4, 7];
const n = arr.length;
console.log(findMinDifference(arr, n));
|
C#
using System;
public class GFG{
public static int findMinDifference( int [] arr, int n)
{
int firstmin = int .MaxValue;
int secondmin = int .MaxValue;
int firstmax = int .MinValue;
int secondmax = int .MinValue;
for ( int i = 0;i < n;i++)
{
if (arr[i] < firstmin)
{
secondmin = firstmin;
firstmin = arr[i];
}
else if (arr[i] < secondmin)
{
secondmin = arr[i];
}
if (arr[i] > firstmax)
{
secondmax = firstmax;
firstmax = arr[i];
}
else if (arr[i] > secondmax)
{
secondmax = arr[i];
}
}
return Math.Min(secondmax - firstmin,firstmax - secondmin);
}
internal static void Main()
{
int [] arr = {1, 3, 4, 7};
int N = arr.Length;
Console.Write(findMinDifference(arr, N));
}
}
|
Time Complexity:- O(N)
Auxiliary Space:- O(1)
Share your thoughts in the comments
Please Login to comment...