📜  从数组中移除一个元素,使得 max – min 为最小值

📅  最后修改于: 2021-10-26 06:36:21             🧑  作者: Mango

给定一个由 N 个正整数组成的数组。任务是从该数组中准确删除一个元素以最小化max(a) – min(a)并打印可能的最小值(max(a) – min(a))
注意: max(a) 表示数组中最大的数a   min(a) 表示数组中最小的数a   .
数组中至少有 2 个元素。
例子:

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

简单的方法:从这里可以看出,我们总是要删除数组的最小值或最大值。我们首先对数组进行排序。排序后,如果我们删除最小元素,差异将是 a[n-1] – a[1]。如果我们删除最大元素,差异将是 a[n-2] – a[0]。我们返回这两个差异中的最小值。
下面是上述方法的实现:

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


Javascript


C++
// C++ implementation of the above approach
#include 
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= 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= 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


Python3
# Python3 implementation of the
# above approach
import sys
 
# function to calculate max-min
def max_min(a, n) :
     
    # There should be at-least two elements
    if (n <= 1) :
        return sys.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)
 
    for i in range(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]
 
    return min((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
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= 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


PHP
= $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
$a = array ( 1, 3, 3, 7 );
$n = sizeof($a);
 
echo(max_min($a, $n));
 
// This code is contributed
// by Mukul Singh
?>


Javascript


输出:
2

时间复杂度: O(n log n)
有效的方法:
一种有效的方法是执行以下操作。
1) 找到第一个最小值和第二个最小值
2)找到第一个最大值和第二个最大值
3) 返回以下两个差值中的最小值。
…..a) 第一个最大值和第二个最小值
…..b) 第二个最大值和第一个最小值

C++

// C++ implementation of the above approach
#include 
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= 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= 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

蟒蛇3

# Python3 implementation of the
# above approach
import sys
 
# function to calculate max-min
def max_min(a, n) :
     
    # There should be at-least two elements
    if (n <= 1) :
        return sys.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)
 
    for i in range(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]
 
    return min((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
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= 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

PHP

= $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
$a = array ( 1, 3, 3, 7 );
$n = sizeof($a);
 
echo(max_min($a, $n));
 
// This code is contributed
// by Mukul Singh
?>

Javascript


输出:
2

时间复杂度: O(n)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程