📌  相关文章
📜  最小化所有可能子数组的最大和最小元素之间的差异

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

最小化所有可能子数组的最大和最小元素之间的差异

给定一个大小为N的数组arr[ ] ,任务是找到 arr[ ] 的所有可能大小的子数组的最大最小元素之间的最小差异。例子:


方法:简单的想法是使用两个循环并检查每个子数组,最大和最小元素之间的最小差异。跟踪差异并返回可能的最小差异。这种方法的时间复杂度是二次的。

有效方法:这个想法是利用我们可以通过仅迭代大小为2的子数组来获得最小差异的事实。
假设我们的子数组中有两个元素。让最大和最小元素之间的差为x 。现在,如果我们在子数组的右侧左侧包含一个元素,则最大元素或最小元素可能会被更新。随着最大或最小元素的更新,此更改最终将使我们的差异x增加

按照以下步骤实施上述方法:

  • 迭代数组并跟踪最小相邻差
  • 打印这个最小差异作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to get the min difference
// between max and min element of all
// possible subarrays
int getMinDifference(int arr[], int n)
{
    // To store the adjacent difference
    int diff;
 
    // To compare with min difference
    int mn = INT_MAX;
 
    for (int i = 1; i < n; i++) {
 
        // Storing adjacent difference
        diff = abs(arr[i] - arr[i - 1]);
 
        // Updating the min difference
        mn = min(diff, mn);
    }
 
    // Returning min difference
    return mn;
}
 
// Driver code
int main()
{
 
    int arr[] = { 2, 6, 15, 7, 6 };
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << getMinDifference(arr, N);
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG{
 
// Function to get the min difference
// between max and min element of all
// possible subarrays
static int getMinDifference(int []arr, int n)
{
    // To store the adjacent difference
    int diff = 0;
 
    // To compare with min difference
    int mn = Integer.MAX_VALUE;
 
    for (int i = 1; i < n; i++) {
 
        // Storing adjacent difference
        diff = Math.abs(arr[i] - arr[i - 1]);
 
        // Updating the min difference
        mn = Math.min(diff, mn);
    }
 
    // Returning min difference
    return mn;
}
 
// Driver code
public static void main (String[] args)
{
 
    int []arr = {2, 6, 15, 7, 6 };
    int N = arr.length;
    System.out.println(getMinDifference(arr, N));
}
}
 
// This code is contributed by shivanisinghss2110


Python3
# Python3 program for the above approach
import sys,math
 
# Function to get the min difference
# between max and min element of all
# possible subarrays
def getMinDifference(arr, n) :
 
    INT_MAX = sys.maxsize;
     
    # To compare with min difference
    mn = INT_MAX;
 
    for i in range(1, n):
 
        # Storing adjacent difference
        diff = abs(arr[i] - arr[i - 1]);
 
        # Updating the min difference
        mn = min(diff, mn);
 
    # Returning min difference
    return mn;
 
# Driver code
if __name__ == "__main__" :
 
 
    arr = [ 2, 6, 15, 7, 6 ];
    N = len(arr);
    print(getMinDifference(arr, N));
 
    # This code is contributed by AnkThon


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to get the min difference
// between max and min element of all
// possible subarrays
static int getMinDifference(int []arr, int n)
{
    // To store the adjacent difference
    int diff = 0;
 
    // To compare with min difference
    int mn = Int32.MaxValue;
 
    for (int i = 1; i < n; i++) {
 
        // Storing adjacent difference
        diff = Math.Abs(arr[i] - arr[i - 1]);
 
        // Updating the min difference
        mn = Math.Min(diff, mn);
    }
 
    // Returning min difference
    return mn;
}
 
// Driver code
public static void Main()
{
 
    int []arr = {2, 6, 15, 7, 6 };
    int N = arr.Length;
    Console.Write(getMinDifference(arr, N));
}
}
 
// This code is contributed by SURENDRA_GANGWAR.


Javascript


输出
1

时间复杂度 O(N),N是元素的数量
辅助空间 O(1)