📌  相关文章
📜  计算一个元素不小于另一个元素的 K 倍的不相交对的最大数量

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

计算一个元素不小于另一个元素的 K 倍的不相交对的最大数量

给定一个数组arr[]和一个正整数K ,任务是找到不相交对(arr[i], arr[j])的最大计数,使得arr[j] ≥ K * arr[i]

例子:

方法:给定的问题可以通过使用双指针方法来解决。请按照以下步骤解决给定的问题:

  1. 按升序对给定数组进行排序。
  2. 将两个变量ij分别初始化为0(N / 2)以及存储对的最大计数的变量count
  3. [0, N/2]范围内遍历给定数组并执行以下步骤:
    • 增加j的值,直到j < Narr[j] < K * arr[i]
    • 如果j的值小于N ,则将对的计数增加1
    • j的值增加1
  4. 完成上述步骤后,打印count的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the maximum count
// of disjoint pairs such that arr[i]
// is at least K*arr[j]
int maximizePairs(int arr[], int n, int k)
{
    // Sort the array
    sort(arr, arr + n);
 
    // Initialize the two pointers
    int i = 0, j = n / 2;
 
    // Stores the total count of pairs
    int count = 0;
 
    for (i = 0; i < n / 2; i++) {
 
        // Increment j until a valid
        // pair is found or end of the
        // array is reached
        while (j < n
               && (k * arr[i]) > arr[j])
            j++;
 
        // If j is not the end of the
        // array, then a valid pair
        if (j < n)
            count++;
 
        j++;
    }
 
    // Return the possible count
    return count;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 9, 4, 7, 3 };
    int N = sizeof(arr) / sizeof(int);
    int K = 2;
    cout << maximizePairs(arr, N, K);
 
    return 0;
}


Java
// Java code for above approach
import java.util.*;
 
class GFG{
 
// Function to find the maximum count
// of disjoint pairs such that arr[i]
// is at least K*arr[j]
static int maximizePairs(int arr[], int n, int k)
{
    // Sort the array
    Arrays.sort(arr);
 
    // Initialize the two pointers
    int i = 0, j = n / 2;
 
    // Stores the total count of pairs
    int count = 0;
 
    for (i = 0; i < n / 2; i++) {
 
        // Increment j until a valid
        // pair is found or end of the
        // array is reached
        while (j < n
               && (k * arr[i]) > arr[j])
            j++;
 
        // If j is not the end of the
        // array, then a valid pair
        if (j < n)
            count++;
 
        j++;
    }
 
    // Return the possible count
    return count;
}
 
// Driver Code
public static void main(String[] args)
 
{
    int arr[] = { 1, 9, 4, 7, 3 };
    int N = arr.length;
    int K = 2;
    System.out.print(maximizePairs(arr, N, K));
}
}
 
// This code is contributed by avijitmondal1998.


Python3
# Python 3 program for the above approach
 
# Function to find the maximum count
# of disjoint pairs such that arr[i]
# is at least K*arr[j]
def maximizePairs(arr, n, k):
   
    # Sort the array
    arr.sort()
 
    # Initialize the two pointers
    i = 0
    j = n // 2
 
    # Stores the total count of pairs
    count = 0
 
    for i in range(n//2):
       
        # Increment j until a valid
        # pair is found or end of the
        # array is reached
        while (j < n and (k * arr[i]) > arr[j]):
            j += 1
 
        # If j is not the end of the
        # array, then a valid pair
        if (j < n):
            count += 1
 
        j += 1
 
    # Return the possible count
    return count
 
# Driver Code
if __name__ == '__main__':
    arr = [1, 9, 4, 7, 3]
    N = len(arr)
    K = 2
    print(maximizePairs(arr, N, K))
     
    # This code is contributed by SURENDRA_GANGWAR.


C#
// C# code for above approach
using System;
 
class GFG{
 
// Function to find the maximum count
// of disjoint pairs such that arr[i]
// is at least K*arr[j]
static int maximizePairs(int []arr, int n, int k)
{
    // Sort the array
    Array.Sort(arr);
 
    // Initialize the two pointers
    int i = 0, j = n / 2;
 
    // Stores the total count of pairs
    int count = 0;
 
    for (i = 0; i < n / 2; i++) {
 
        // Increment j until a valid
        // pair is found or end of the
        // array is reached
        while (j < n
               && (k * arr[i]) > arr[j])
            j++;
 
        // If j is not the end of the
        // array, then a valid pair
        if (j < n)
            count++;
 
        j++;
    }
 
    // Return the possible count
    return count;
}
 
// Driver Code
public static void Main(String[] args)
 
{
    int []arr = { 1, 9, 4, 7, 3 };
    int N = arr.Length;
    int K = 2;
    Console.Write(maximizePairs(arr, N, K));
}
}
 
// This code is contributed by shivanisinghss2110


Javascript


输出:
2

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