📌  相关文章
📜  通过恰好一次删除来最小化任何一对数组元素之间的最大差异

📅  最后修改于: 2022-05-13 01:56:08.486000             🧑  作者: Mango

通过恰好一次删除来最小化任何一对数组元素之间的最大差异

给定一个由N个整数 (N > 2) 组成的数组arr[] ,任务是通过仅删除一个元素来最小化任何一对元素(arr[i], arr[j])之间的最大差异。

例子:

朴素方法:解决给定问题的最简单方法是逐个删除每个元素并检查哪个元素使每对元素之间的最大差异最小。

时间复杂度: O(N 3 )
辅助空间: O(N)

有效方法:上述方法也可以通过观察最大差异等于给定数组的最大和最小元素之间的差异这一事实来优化。因此,删除最大元素或删除最小元素总是给出最佳答案。

因此,想法是按升序对给定数组进行排序,并打印值(arr[N – 2] – arr[0])(arr[N – 1] – arr[1])的最小值作为结果最小化最大差异。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
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


输出:
3

时间复杂度: O(N*log N)
辅助空间: O(N)