📌  相关文章
📜  来自给定大小的子序列的所有对的最小差异的最大值

📅  最后修改于: 2021-09-02 06:36:46             🧑  作者: Mango

给定一个大小为N的整数数组A[ ] ,任务是找到一个大小为B的子序列,使得它们中任何两个之间的最小差异最大,并打印这个最大的最小差异。

例子:

天真的方法:
解决这个问题最简单的方法是生成所有可能的大小为B 的子序列,并找到所有可能对之间的最小差异。最后,在所有最小差异中找到最大值。

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

有效的方法:
按照以下步骤使用二分搜索优化上述方法:

  • 将搜索空间设置为从0到数组中的最大元素( maxm )
  • 对于每个计算出的mid ,检查是否有可能得到一个大小为B的子序列,它在任何等于mid 的对中的差异最小
  • 如果可能,则将mid存储在变量中并在右半部分找到更好的答案并丢弃 mid 的左半部分
  • 否则,遍历中间的左半部分,以检查是否存在具有较小对最小差异的子序列。
  • 最后,在终止二分搜索后,打印最高的mid ,找到任何对的最小差异等于mid 的子序列。

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
#include 
using namespace std;
 
// Function to check a subsequence can
// be formed with min difference mid
bool can_place(int A[], int n,
               int B, int mid)
{
    int count = 1;
    int last_position = A[0];
 
    // If a subsequence of size B
    // with min diff = mid is possible
    // return true else false
    for (int i = 1; i < n; i++) {
 
        if (A[i] - last_position
            >= mid) {
            last_position = A[i];
            count++;
            if (count == B) {
                return true;
            }
        }
    }
    return false;
}
 
// Function to find the maximum of
// all minimum difference of pairs
// possible among the subsequence
int find_min_difference(int A[],
                        int n, int B)
{
 
    // Sort the Array
    sort(A, A + n);
 
    // Stores the boundaries
    // of the search space
    int s = 0;
    int e = A[n - 1] - A[0];
 
    // Store the answer
    int ans = 0;
 
    // Binary Search
    while (s <= e) {
 
        long long int mid = (s + e) / 2;
 
        // If subsequence can be formed
        // with min diff mid and size B
        if (can_place(A, n, B, mid)) {
            ans = mid;
 
            // Right half
            s = mid + 1;
        }
        else {
 
            // Left half
            e = mid - 1;
        }
    }
 
    return ans;
}
 
// Driver Code
int main()
{
    int A[] = { 1, 2, 3, 5 };
    int n = sizeof(A) / sizeof(A[0]);
    int B = 3;
 
    int min_difference
        = find_min_difference(A, n, B);
    cout << min_difference;
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
 
// Function to check a subsequence can
// be formed with min difference mid
static boolean can_place(int A[], int n,
                         int B, int mid)
{
    int count = 1;
    int last_position = A[0];
 
    // If a subsequence of size B
    // with min diff = mid is possible
    // return true else false
    for(int i = 1; i < n; i++)
    {
        if (A[i] - last_position >= mid)
        {
            last_position = A[i];
            count++;
            if (count == B)
            {
                return true;
            }
        }
    }
    return false;
}
 
// Function to find the maximum of
// all minimum difference of pairs
// possible among the subsequence
static int find_min_difference(int A[],
                        int n, int B)
{
 
    // Sort the Array
    Arrays.sort(A);
 
    // Stores the boundaries
    // of the search space
    int s = 0;
    int e = A[n - 1] - A[0];
 
    // Store the answer
    int ans = 0;
 
    // Binary Search
    while (s <= e)
    {
        int mid = (s + e) / 2;
 
        // If subsequence can be formed
        // with min diff mid and size B
        if (can_place(A, n, B, mid))
        {
            ans = mid;
 
            // Right half
            s = mid + 1;
        }
        else
        {
             
            // Left half
            e = mid - 1;
        }
    }
    return ans;
}
 
// Driver Code
public static void main(String[] args)
{
    int A[] = { 1, 2, 3, 5 };
    int n = A.length;
    int B = 3;
 
    int min_difference = find_min_difference(A, n, B);
     
    System.out.print(min_difference);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program to implement
# the above approach
 
# Function to check a subsequence can
# be formed with min difference mid
def can_place(A, n, B, mid):
 
    count = 1
    last_position = A[0]
 
    # If a subsequence of size B
    # with min diff = mid is possible
    # return true else false
    for i in range(1, n):
        if (A[i] - last_position >= mid):
            last_position = A[i]
            count = count + 1
             
            if (count == B):
                return bool(True)
                 
    return bool(False)
 
# Function to find the maximum of
# all minimum difference of pairs
# possible among the subsequence
def find_min_difference(A, n, B):
 
    # Sort the Array
    A.sort()
 
    # Stores the boundaries
    # of the search space
    s = 0
    e = A[n - 1] - A[0]
 
    # Store the answer
    ans = 0
 
    # Binary Search
    while (s <= e):
        mid = (int)((s + e) / 2)
 
        # If subsequence can be formed
        # with min diff mid and size B
        if (can_place(A, n, B, mid)):
            ans = mid
 
            # Right half
            s = mid + 1
         
        else:
 
            # Left half
            e = mid - 1
     
    return ans
 
# Driver code
A = [ 1, 2, 3, 5 ]
n = len(A)
B = 3
 
min_difference = find_min_difference(A, n, B)
 
print(min_difference)
 
# This code is contributed by divyeshrabadiya07


C#
// C# program to implement
// the above approach
using System;
class GFG{
  
// Function to check a subsequence can
// be formed with min difference mid
static bool can_place(int[] A, int n,
                      int B, int mid)
{
    int count = 1;
    int last_position = A[0];
  
    // If a subsequence of size B
    // with min diff = mid is possible
    // return true else false
    for(int i = 1; i < n; i++)
    {
        if (A[i] - last_position >= mid)
        {
            last_position = A[i];
            count++;
            if (count == B)
            {
                return true;
            }
        }
    }
    return false;
}
  
// Function to find the maximum of
// all minimum difference of pairs
// possible among the subsequence
static int find_min_difference(int[] A,
                        int n, int B)
{
  
    // Sort the Array
    Array.Sort(A);
  
    // Stores the boundaries
    // of the search space
    int s = 0;
    int e = A[n - 1] - A[0];
  
    // Store the answer
    int ans = 0;
  
    // Binary Search
    while (s <= e)
    {
        int mid = (s + e) / 2;
  
        // If subsequence can be formed
        // with min diff mid and size B
        if (can_place(A, n, B, mid))
        {
            ans = mid;
  
            // Right half
            s = mid + 1;
        }
        else
        {
              
            // Left half
            e = mid - 1;
        }
    }
    return ans;
}
  
// Driver Code
public static void Main(string[] args)
{
    int[] A = { 1, 2, 3, 5 };
    int n = A.Length;
    int B = 3;
  
    int min_difference = find_min_difference(A, n, B);
      
    Console.Write(min_difference);
}
}
  
// This code is contributed by rock_cool


Javascript


输出:
2

时间复杂度: O(NlogN)
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live