Given an array arr[] of size n ≥ 3, the task is to find the minimum possible difference between the the maximum and the minimum element from the array after removing one element.
Examples:
Input: arr[] = {1, 2, 3}
Output: 1
Removing 1 will give 3 – 2 = 1
Removing 2, 3 – 1 = 2
And removing 3 will result in 2 – 1 = 1Input: arr[] = {1, 2, 4, 3, 4}
Output: 2
Naive Approach: It is clear that to have an effect on the difference only the minimum or the maximum element has to be removed.
- Sort the array.
- Remove the minimum, store diff1 = arr[n – 1] – arr[1].
- Remove the maximum, and diff2 = arr[n – 2] – arr[0].
- Print min(diff1, diff2) in the end.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the minimum required difference int findMinDifference(int arr[], int n) { // Sort the given array sort(arr, arr + n); // When minimum element is removed int diff1 = arr[n - 1] - arr[1]; // When maximu element is removed int diff2 = arr[n - 2] - arr[0]; // Return the minimum of diff1 and diff2 return min(diff1, diff2); } // Driver Code int main() { int arr[] = { 1, 2, 4, 3, 4 }; int n = sizeof(arr) / sizeof(arr[0]); cout << findMinDifference(arr, n); return 0; } |
Python3
# Python3 implementation of the approach # Function to return the minimum # required difference def findMinDifference(arr, n) : # Sort the given array arr.sort() # When minimum element is removed diff1 = arr[n - 1] - arr[1] # When maximu element is removed diff2 = arr[n - 2] - arr[0] # Return the minimum of diff1 and diff2 return min(diff1, diff2) # Driver Code if __name__ == "__main__" : arr = [ 1, 2, 4, 3, 4 ] n = len(arr) print(findMinDifference(arr, n)) # This code is contributed by Ryuga |
2
Efficient Approach: In order to find the min, secondMin, max and secondMax elements from the array. We don’t need to sort the array, it can be done in a single array traversal.
Below is the implementation of the above approach:
// Java implementation of the approach public class GFG { // Function to return the minimum required difference static int findMinDifference(int arr[], int n) { int min, secondMin, max, secondMax; min = secondMax = (arr[0] < arr[1]) ? arr[0] : arr[1]; max = secondMin = (arr[0] < arr[1]) ? arr[1] : arr[0]; for (int i = 2; i < n; i++) { // If current element is greater than max if (arr[i] > max) { // max will become secondMax secondMax = max; // Update the max max = arr[i]; } // If current element is greater than secondMax // but smaller than max else if (arr[i] > secondMax) { // Update the secondMax secondMax = arr[i]; } // If current element is smaller than min else if (arr[i] < min) { // min will become secondMin secondMin = min; // Update the min min = arr[i]; } // If current element is smaller than secondMin // but greater than min else if (arr[i] < secondMin) { // Update the secondMin secondMin = arr[i]; } } // Minimum of the two possible differences int diff = Math.min(max - secondMin, secondMax - min); return diff; } // Driver code public static void main(String[] args) { int arr[] = { 1, 2, 4, 3, 4 }; int n = arr.length; System.out.println(findMinDifference(arr, n)); } } |
2
Recommended Posts:
- Minimize the difference between the maximum and minimum values of the modified array
- Minimize the difference between minimum and maximum elements
- Maximum removal from array when removal time >= waiting time
- Minimize the maximum difference between the heights
- Choose k array elements such that difference of maximum and minimum is minimized
- Queries to add, remove and return the difference of maximum and minimum.
- Maximum and minimum of an array using minimum number of comparisons
- Maximum possible difference of two subsets of an array
- Maximum sum of absolute difference of an array
- Sum of minimum absolute difference of each array element
- Sum of minimum difference between consecutive elements of an array
- Find the sum of maximum difference possible from all subset of a given array.
- Maximum difference between first and last indexes of an element in array
- Maximum adjacent difference in an array in its sorted form
- Construct array having X subsequences with maximum difference smaller than d
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.
Improved By : Ryuga



