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> using namespace std; // function to calculate max-min int max_min(int a[], int n) { sort(a, a + n); return min(a[n - 2] - a[0], a[n - 1] - a[1]); } // Driver code int main() { int a[] = { 1, 3, 3, 7 }; int n = sizeof(a) / sizeof(a[0]); cout << max_min(a, n); return 0; } |
Java
// Java implementation of the above approach import java.util.*; class GFG { // function to calculate max-min static int max_min(int a[], int n) { Arrays.sort(a); return Math.min(a[n - 2] - a[0], a[n - 1] - a[1]); } // Driver code public static void main(String []args) { int a[] = { 1, 3, 3, 7 }; int n = 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 def max_min(a, n): a.sort() return min(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 using System; class GFG { // function to calculate max-min static int max_min(int []a, int n) { Array.Sort(a); return Math.Min(a[n - 2] - a[0], a[n - 1] - a[1]); } // Driver code public static void Main() { int []a = { 1, 3, 3, 7 }; int n = 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 function max_min(&$a, $n) { sort($a); return min($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 ?> |
2
Time Complexity: O(n log n)
Efficient Approach:
An efficient approach is to do following.
1) Find first minimum and second minmum
2) Find first maximum and second maximum
3) Return the minimum of following two differences.
…..a) First maximum and second minimum
…..b) Second maximum and first minimum
C++
// C++ implementation of the above approach #include <bits/stdc++.h> using namespace std; // function to calculate max-min int max_min(int a[], int n) { // There should be at-least two elements if (n <= 1) return INT_MAX; // To store first and second minimums int f_min = a[0], s_min = INT_MAX; // To store first and second maximums int f_max = a[0], s_max = INT_MIN; for (int i = 1; i<n ;i++) { if (a[i] <= f_min) { s_min = f_min; f_min = a[i]; } else if (a[i] < s_min) { s_min = a[i]; } if (a[i] >= f_max) { s_max = f_max; f_max = a[i]; } else if (a[i] > s_max) { s_max = a[i]; } } return min((f_max - s_min), (s_max - f_min)); } // Driver code int main() { int a[] = { 1, 3, 3, 7 }; int n = sizeof(a) / sizeof(a[0]); cout << max_min(a, n); return 0; } |
Java
// Java implementation of the above approach class GFG { // function to calculate max-min static int max_min(int a[], int n) { // There should be at-least two elements if (n <= 1) return Integer.MAX_VALUE; // To store first and second minimums int f_min = a[0], s_min = Integer.MAX_VALUE; // To store first and second maximums int f_max = a[0], s_max = Integer.MIN_VALUE; for (int i = 1; i<n ;i++) { if (a[i] <= f_min) { s_min = f_min; f_min = a[i]; } else if (a[i] < s_min) { s_min = a[i]; } if (a[i] >= f_max) { s_max = f_max; f_max = a[i]; } else if (a[i] > s_max) { s_max = a[i]; } } return Math.min((f_max - s_min), (s_max - f_min)); } // Driver code public static void main(String []args) { int a[] = { 1, 3, 3, 7 }; int n = a.length; System.out.println(max_min(a, n)); } } // This code is contributed // by ihritik |
C#
// C# implementation of the above approach using System; class GFG { // function to calculate max-min static int max_min(int []a, int n) { // There should be at-least two elements if (n <= 1) return Int32.MaxValue; // To store first and second minimums int f_min = a[0], s_min = Int32.MaxValue; // To store first and second maximums int f_max = a[0], s_max = Int32.MinValue; for (int i = 1; i<n ;i++) { if (a[i] <= f_min) { s_min = f_min; f_min = a[i]; } else if (a[i] < s_min) { s_min = a[i]; } if (a[i] >= f_max) { s_max = f_max; f_max = a[i]; } else if (a[i] > s_max) { s_max = a[i]; } } return Math.Min((f_max - s_min), (s_max - f_min)); } // Driver code public static void Main() { int []a = { 1, 3, 3, 7 }; int n = a.Length; Console.WriteLine(max_min(a, n)); } } // This code is contributed // by ihritik |
2
Time Complexity: O(n)
Recommended Posts:
- Remove minimum number of elements such that no common element exist in both array
- Remove minimum elements from array such that no three consecutive element are either increasing or decreasing
- Remove array end element to maximize the sum of product
- Remove minimum elements from array so that max <= 2 * min
- Minimum operations required to remove an array
- Minimum sum of differences with an element in an array
- Sum of minimum absolute difference of each array element
- Minimum array element changes to make its elements 1 to N
- Sum and Product of minimum and maximum element of an Array
- Program to find the minimum (or maximum) element of an array
- Find the minimum element in a sorted and rotated array
- Minimum and Maximum element of an array which is divisible by a given number k
- Leftmost and rightmost indices of the maximum and the minimum element of an array
- Search an element in an unsorted array using minimum number of comparisons
- Minimum pair sum operations to make array each element divisible by 4
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.



