给定一个大小为n ≥ 3的数组arr[] ,任务是在删除一个元素后从数组中找到最大和最小元素之间可能的最小差异。
例子:
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 = 1
Input: arr[] = {1, 2, 4, 3, 4}
Output: 2
朴素的方法:很明显,要对差异产生影响,只需删除最小或最大元素。
- 对数组进行排序。
- 删除最小值,存储diff1 = arr[n – 1] – arr[1] 。
- 去除最大值,然后diff2 = arr[n – 2] – arr[0] 。
- 最后打印min(diff1, diff2) 。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
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 maximum 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;
}
Java
// Java implementation of the approach
import java.util.*;
class solution
{
// Function to return the minimum required difference
static int findMinDifference(int arr[], int n)
{
// Sort the given array
Arrays.sort(arr);
// When minimum element is removed
int diff1 = arr[n - 1] - arr[1];
// When maximum element is removed
int diff2 = arr[n - 2] - arr[0];
// Return the minimum of diff1 and diff2
return Math.min(diff1, diff2);
}
// Driver Code
public static void main(String args[])
{
int arr[] = { 1, 2, 4, 3, 4 };
int n = arr.length;
System.out.print(findMinDifference(arr, n));
}
}
// This code is contributed by
// Sanjit_Prasad
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 maximum 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
C#
// C# implementation of the approach
using System;
public class GFG{
// Function to return the minimum required difference
static int findMinDifference(int []arr, int n)
{
// Sort the given array
Array.Sort(arr);
// When minimum element is removed
int diff1 = arr[n - 1] - arr[1];
// When maximum element is removed
int diff2 = arr[n - 2] - arr[0];
// Return the minimum of diff1 and diff2
return Math.Min(diff1, diff2);
}
// Driver Code
static public void Main (){
int []arr = { 1, 2, 4, 3, 4 };
int n = arr.Length;
Console.Write(findMinDifference(arr, n));
}
}
// This code is contributed by Sachin..
PHP
Javascript
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the minimum required difference
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 = min(max__ - secondMin, secondMax - min__);
return diff;
}
// Driver code
int main()
{
int arr[] = { 1, 2, 4, 3, 4 };
int n = sizeof(arr)/sizeof(arr[0]);
cout << (findMinDifference(arr, n));
}
// This code is contributed by
// Shashank_Sharma
Java
// 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));
}
}
Python3
# Python 3 implementation of the approach
# Function to return the minimum
# required difference
def findMinDifference(arr, n):
if(arr[0] < arr[1]):
min__ = secondMax = arr[0]
else:
min__ = secondMax = arr[1]
if(arr[0] < arr[1]):
max__ = secondMin = arr[1]
else:
max__ = secondMin = arr[0]
for i in range(2, n):
# 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
elif (arr[i] > secondMax):
# Update the secondMax
secondMax = arr[i]
# If current element is smaller than min
elif(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
elif(arr[i] < secondMin):
# Update the secondMin
secondMin = arr[i]
# Minimum of the two possible
# differences
diff = min(max__ - secondMin,
secondMax - min__)
return diff
# Driver code
if __name__ == '__main__':
arr = [1, 2, 4, 3, 4]
n = len(arr)
print(findMinDifference(arr, n))
# This code is contributed by
# Surendra_Gangwar
C#
using System;
// C# 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()
{
int []arr = { 1, 2, 4, 3, 4 };
int n = arr.Length;
Console.WriteLine(findMinDifference(arr, n));
}
}
// This code is contributed by 29AjayKumar
PHP
$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
$diff = min($max__ - $secondMin,
$secondMax - $min__);
return $diff;
}
// Driver code
$arr = array( 1, 2, 4, 3, 4 );
$n = count($arr);
print(findMinDifference($arr, $n));
// This code is contributed by mits
?>
Javascript
输出:
2
有效的方法:为了从数组中找到min 、 secondMin 、 max和secondMax元素。我们不需要对数组进行排序,它可以在单个数组遍历中完成。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the minimum required difference
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 = min(max__ - secondMin, secondMax - min__);
return diff;
}
// Driver code
int main()
{
int arr[] = { 1, 2, 4, 3, 4 };
int n = sizeof(arr)/sizeof(arr[0]);
cout << (findMinDifference(arr, n));
}
// This code is contributed by
// Shashank_Sharma
Java
// 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));
}
}
蟒蛇3
# Python 3 implementation of the approach
# Function to return the minimum
# required difference
def findMinDifference(arr, n):
if(arr[0] < arr[1]):
min__ = secondMax = arr[0]
else:
min__ = secondMax = arr[1]
if(arr[0] < arr[1]):
max__ = secondMin = arr[1]
else:
max__ = secondMin = arr[0]
for i in range(2, n):
# 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
elif (arr[i] > secondMax):
# Update the secondMax
secondMax = arr[i]
# If current element is smaller than min
elif(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
elif(arr[i] < secondMin):
# Update the secondMin
secondMin = arr[i]
# Minimum of the two possible
# differences
diff = min(max__ - secondMin,
secondMax - min__)
return diff
# Driver code
if __name__ == '__main__':
arr = [1, 2, 4, 3, 4]
n = len(arr)
print(findMinDifference(arr, n))
# This code is contributed by
# Surendra_Gangwar
C#
using System;
// C# 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()
{
int []arr = { 1, 2, 4, 3, 4 };
int n = arr.Length;
Console.WriteLine(findMinDifference(arr, n));
}
}
// This code is contributed by 29AjayKumar
PHP
$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
$diff = min($max__ - $secondMin,
$secondMax - $min__);
return $diff;
}
// Driver code
$arr = array( 1, 2, 4, 3, 4 );
$n = count($arr);
print(findMinDifference($arr, $n));
// This code is contributed by mits
?>
Javascript
输出:
2