Given an array consisting of N positive integer numbers. The task is to remove exactly one element from this array to minimize max(a) – min(a) and print the minimum possible (max(a) – min(a)).
Note: max(a) means largest number in array and min(a) means smallest number in array .
There are at least 2 elements in the array.
Examples:
Input: arr[] = {1, 3, 3, 7}
Output: 2
Remove 7, then max(a) will be 3 and min(a) will be 1.
So our answer will be 3-1 = 2.
Input: arr[] = {1, 1000}
Output: 0
Remove either 1 or 1000, then our answer will 1-1 =0 or
1000-1000=0
Simple Approach: Here it can be seen that we always have to remove either minimum or maximum of the array. We first sort the array. After sorting, if we remove minimum element, the difference would be a[n-1] – a[1]. And if we remove the maximum element, difference would be a[n-2] – a[0]. We return minimum of these two differences.
Below is the implementation of the above approach:
C++
// C++ implementation of the above approach
#include <bits/stdc++.h>
usingnamespacestd;
// function to calculate max-min
intmax_min(inta[], intn)
{
sort(a, a + n);
returnmin(a[n - 2] - a[0], a[n - 1] - a[1]);
}
// Driver code
intmain()
{
inta[] = { 1, 3, 3, 7 };
intn = sizeof(a) / sizeof(a[0]);
cout << max_min(a, n);
return0;
}
Java
// Java implementation of the above approach
importjava.util.*;
classGFG
{
// function to calculate max-min
staticintmax_min(inta[], intn)
{
Arrays.sort(a);
returnMath.min(a[n - 2] - a[0], a[n - 1] - a[1]);
}
// Driver code
publicstaticvoidmain(String []args)
{
inta[] = { 1, 3, 3, 7};
intn = a.length;
System.out.println(max_min(a, n));
}
}
// This code is contributed
// by ihritik
Python3
# Python3 implementation of the
# above approach
# function to calculate max-min
defmax_min(a, n):
a.sort()
returnmin(a[n -2] -a[0],
a[n -1] -a[1])
# Driver code
a =[1, 3, 3, 7]
n =len(a)
print(max_min(a, n))
# This code is contributed
# by sahishelangia
C#
// C# implementation of the above approach
usingSystem;
classGFG
{
// function to calculate max-min
staticintmax_min(int[]a, intn)
{
Array.Sort(a);
returnMath.Min(a[n - 2] - a[0], a[n - 1] - a[1]);
}
// Driver code
publicstaticvoidMain()
{
int[]a = { 1, 3, 3, 7 };
intn = a.Length;
Console.WriteLine(max_min(a, n));
}
}
// This code is contributed
// by ihritik
PHP
<?php
// PHP implementation of the above approach
// function to calculate max-min
functionmax_min(&$a, $n)
{
sort($a);
returnmin($a[$n- 2] - $a[0],
$a[$n- 1] - $a[1]);
}
// Driver code
$a= array(1, 3, 3, 7);
$n= sizeof($a);
echo(max_min($a, $n));
// This code is contributed by Shivi_Aggarwal
?>
Javascript
<script>
// Javascript program of the above approach
// function to calculate max-min
functionmax_min(a, n)
{
a.sort();
returnMath.min(a[n - 2] - a[0], a[n - 1] - a[1]);
}
// Driver code
let a = [ 1, 3, 3, 7 ];
let n = a.length;
document.write(max_min(a, n));
</script>
Output
2
Time Complexity:O(n log n) Auxiliary Space: O(1)
Efficient Approach:
An efficient approach is to do following.
Find first minimum and second minimum
Find first maximum and second maximum
Return the minimum of following two differences.
First maximum and second minimum
Second maximum and first minimum
C++
// C++ implementation of the above approach
#include <bits/stdc++.h>
usingnamespacestd;
// function to calculate max-min
intmax_min(inta[], intn)
{
// There should be at-least two elements
if(n <= 1)
returnINT_MAX;
// To store first and second minimums
intf_min = a[0], s_min = INT_MAX;
// To store first and second maximums
intf_max = a[0], s_max = INT_MIN;
for(inti = 1; i<n ;i++)
{
if(a[i] <= f_min)
{
s_min = f_min;
f_min = a[i];
}
elseif(a[i] < s_min)
{
s_min = a[i];
}
if(a[i] >= f_max)
{
s_max = f_max;
f_max = a[i];
}
elseif(a[i] > s_max)
{
s_max = a[i];
}
}
returnmin((f_max - s_min), (s_max - f_min));
}
// Driver code
intmain()
{
inta[] = { 1, 3, 3, 7 };
intn = sizeof(a) / sizeof(a[0]);
cout << max_min(a, n);
return0;
}
Java
// Java implementation of the above approach
classGFG
{
// function to calculate max-min
staticintmax_min(inta[], intn)
{
// There should be at-least two elements
if(n <= 1)
returnInteger.MAX_VALUE;
// To store first and second minimums
intf_min = a[0], s_min = Integer.MAX_VALUE;
// To store first and second maximums
intf_max = a[0], s_max = Integer.MIN_VALUE;
for(inti = 1; i<n ;i++)
{
if(a[i] <= f_min)
{
s_min = f_min;
f_min = a[i];
}
elseif(a[i] < s_min)
{
s_min = a[i];
}
if(a[i] >= f_max)
{
s_max = f_max;
f_max = a[i];
}
elseif(a[i] > s_max)
{
s_max = a[i];
}
}
returnMath.min((f_max - s_min), (s_max - f_min));
}
// Driver code
publicstaticvoidmain(String []args)
{
inta[] = { 1, 3, 3, 7};
intn = a.length;
System.out.println(max_min(a, n));
}
}
// This code is contributed
// by ihritik
Python3
# Python3 implementation of the
# above approach
importsys
# function to calculate max-min
defmax_min(a, n) :
# There should be at-least two elements
if(n <=1) :
returnsys.maxsize
# To store first and second minimums
f_min =a[0]
s_min =sys.maxsize
# To store first and second maximums
f_max =a[0]
s_max =-(sys.maxsize -1)
fori inrange(n) :
if(a[i] <=f_min) :
s_min =f_min
f_min =a[i]
elif(a[i] < s_min) :
s_min =a[i]
if(a[i] >=f_max) :
s_max =f_max
f_max =a[i]
elif(a[i] > s_max) :
s_max =a[i]
returnmin((f_max -s_min), (s_max -f_min))
# Driver code
if__name__ =="__main__":
a =[ 1, 3, 3, 7]
n =len(a)
print(max_min(a, n))
# This code is contributed by Ryuga
C#
// C# implementation of the above approach
usingSystem;
classGFG
{
// function to calculate max-min
staticintmax_min(int[]a, intn)
{
// There should be at-least two elements
if(n <= 1)
returnInt32.MaxValue;
// To store first and second minimums
intf_min = a[0], s_min = Int32.MaxValue;
// To store first and second maximums
intf_max = a[0], s_max = Int32.MinValue;
for(inti = 1; i<n ;i++)
{
if(a[i] <= f_min)
{
s_min = f_min;
f_min = a[i];
}
elseif(a[i] < s_min)
{
s_min = a[i];
}
if(a[i] >= f_max)
{
s_max = f_max;
f_max = a[i];
}
elseif(a[i] > s_max)
{
s_max = a[i];
}
}
returnMath.Min((f_max - s_min), (s_max - f_min));
}
// Driver code
publicstaticvoidMain()
{
int[]a = { 1, 3, 3, 7 };
intn = a.Length;
Console.WriteLine(max_min(a, n));
}
}
// This code is contributed
// by ihritik
PHP
<?php
// PHP implementation of the above approach
// function to calculate max-min
functionmax_min($a, $n)
{
// There should be at-least
// two elements
if($n<= 1)
returnPHP_INT_MAX;
// To store first and second minimums
$f_min= $a[0];
$s_min= PHP_INT_MAX;
// To store first and second maximums
$f_max= $a[0];
$s_max= ~PHP_INT_MAX;
for($i= 1; $i< $n;$i++)
{
if($a[$i] <= $f_min)
{
$s_min= $f_min;
$f_min= $a[$i];
}
elseif($a[$i] < $s_min)
{
$s_min= $a[$i];
}
if($a[$i] >= $f_max)
{
$s_max= $f_max;
$f_max= $a[$i];
}
elseif($a[$i] > $s_max)
{
$s_max= $a[$i];
}
}
returnmin(($f_max- $s_min),
($s_max- $f_min));
}
// Driver code
$a= array( 1, 3, 3, 7 );
$n= sizeof($a);
echo(max_min($a, $n));
// This code is contributed
// by Mukul Singh
?>
Javascript
<script>
// JavaScript implementation of the above approach
// function to calculate max-min
functionmax_min(a, n)
{
// There should be at-least two elements
if(n <= 1)
returnNumber.MAX_VALUE;
// To store first and second minimums
let f_min = a[0], s_min = Number.MAX_VALUE;
// To store first and second maximums
let f_max = a[0], s_max = Number.MIN_VALUE;
for(let i = 1; i<n ;i++)
{
if(a[i] <= f_min)
{
s_min = f_min;
f_min = a[i];
}
elseif(a[i] < s_min)
{
s_min = a[i];
}
if(a[i] >= f_max)
{
s_max = f_max;
f_max = a[i];
}
elseif(a[i] > s_max)
{
s_max = a[i];
}
}
returnMath.min((f_max - s_min), (s_max - f_min));
}
let a = [ 1, 3, 3, 7 ];
let n = a.length;
document.write(max_min(a, n));
</script>
Output
2
Time Complexity: O(n) Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!