📌  相关文章
📜  来自数组 A 和 B 的对的计数,使得 A 中的元素在该索引处大于 B 中的元素

📅  最后修改于: 2021-09-07 05:09:21             🧑  作者: Mango

给定两个大小为N 的数组A[]B[] ,任务是计算对的最大数量,其中每对包含来自每个数组的一个,使得A[i] > B[i] 。此外,阵列 A 可以重新排列任意次数。

例子:

方法:思路是使用堆的概念。由于 B[] 的排列在问题中无关紧要,我们可以对两个数组执行最大堆。在执行 max heap 并将值存储在两个不同的堆中后,遍历对应于 A[] 和 B[] 的堆以计算满足给定条件A[i] > B[i]的索引数量。

下面是上述方法的实现:

C++14
// C++ program to find the maximum count of
// values that follow the given condition
#include
using namespace std;
 
// Function to find the maximum count of
// values that follow the given condition
int check(int A[], int B[], int N)
{
 
    // Initializing the max-heap for the array A[]
    priority_queue  pq1,pq2;
 
    // Adding the values of A[] into max heap
    for (int i = 0; i < N; i++) {
        pq1.push(A[i]);
    }
 
    // Adding the values of B[] into max heap
    for (int i = 0; i < N; i++) {
        pq2.push(B[i]);
    }
 
    // Counter variable
    int c = 0;
 
    // Loop to iterate through the heap
    for (int i = 0; i < N; i++) {
 
        // Comparing the values at the top.
        // If the value of heap A[] is greater,
        // then counter is incremented
        if (pq1.top()>pq2.top()) {
            c++;
            pq1.pop();
            pq2.pop();
        }
        else {
            if (pq2.size() == 0) {
                break;
            }
            pq2.pop();
        }
    }
    return (c);
}
 
// Driver code
int main()
{
    int A[] = { 10, 3, 7, 5, 8 };
    int B[] = { 8, 6, 2, 5, 9 };
    int N = sizeof(A)/sizeof(A[0]);
 
    cout<<(check(A, B, N));
}
 
// This code is contributed by mohit kumar 29


Java
// Java program to find the maximum count of
// values that follow the given condition
 
import java.util.*;
public class GFG {
 
    // Function to find the maximum count of
    // values that follow the given condition
    static int check(int A[], int B[], int N)
    {
 
        // Initializing the max-heap for the array A[]
        PriorityQueue pq1
            = new PriorityQueue(
Collections.reverseOrder());
 
        // Initializing the max-heap for the array B[]
        PriorityQueue pq2
            = new PriorityQueue(
Collections.reverseOrder());
 
        // Adding the values of A[] into max heap
        for (int i = 0; i < N; i++) {
            pq1.add(A[i]);
        }
 
        // Adding the values of B[] into max heap
        for (int i = 0; i < N; i++) {
            pq2.add(B[i]);
        }
 
        // Counter variable
        int c = 0;
 
        // Loop to iterate through the heap
        for (int i = 0; i < N; i++) {
 
            // Comparing the values at the top.
            // If the value of heap A[] is greater,
            // then counter is incremented
            if (pq1.peek().compareTo(pq2.peek()) == 1) {
                c++;
                pq1.poll();
                pq2.poll();
            }
            else {
                if (pq2.size() == 0) {
                    break;
                }
                pq2.poll();
            }
        }
        return (c);
    }
 
    // Driver code
    public static void main(String args[])
    {
        int A[] = { 10, 3, 7, 5, 8 };
        int B[] = { 8, 6, 2, 5, 9 };
        int N = A.length;
 
        System.out.println(check(A, B, N));
    }
}


Python3
# Python3 program to find the maximum count of
# values that follow the given condition
import heapq
 
# Function to find the maximum count of
# values that follow the given condition
def check(A, B,N):
 
    # Initializing the max-heap for the array A[]
    pq1 = []
    pq2 = []
 
    # Adding the values of A[] into max heap
    for i in range(N):
        heapq.heappush(pq1,-A[i])
 
    # Adding the values of B[] into max heap
    for i in range(N):
        heapq.heappush(pq2,-B[i])
 
    # Counter variable
    c = 0
 
    # Loop to iterate through the heap
    for i in range(N):
 
        # Comparing the values at the top.
        # If the value of heap A[] is greater,
        # then counter is incremented
        if -pq1[0] > -pq2[0]:
            c += 1
            heapq.heappop(pq1)
            heapq.heappop(pq2)
 
        else:
            if len(pq2) == 0:
                break
            heapq.heappop(pq2)
    return (c)
 
# Driver code
A = [ 10, 3, 7, 5, 8 ]
B = [ 8, 6, 2, 5, 9 ]
N = len(A)
 
print(check(A, B, N))
 
# This code is contributed by apurva raj


C#
// C# program to find the maximum count of
// values that follow the given condition
using System;
using System.Collections.Generic;
 
class GFG{
     
// Function to find the maximum count of
// values that follow the given condition
static int check(int[] A, int[] B, int N)
{
     
    // Initializing the max-heap for the array A[]
    List pq1 = new List();
     
    // Initializing the max-heap for the array B[]
    List pq2 = new List();
     
    // Adding the values of A[] into max heap
    for(int i = 0; i < N; i++)
    {
        pq1.Add(A[i]);
    }
     
    // Adding the values of B[] into max heap
    for(int i = 0; i < N; i++)
    {
        pq2.Add(B[i]);
    }
    pq1.Sort();
    pq1.Reverse();
    pq2.Sort();
    pq2.Reverse();
     
    // Counter variable
    int c = 0;
     
    // Loop to iterate through the heap
    for(int i = 0; i < N; i++)
    {
         
        // Comparing the values at the top.
        // If the value of heap A[] is greater,
        // then counter is incremented
        if (pq1[0] > pq2[0])
        {
            c++;
            pq1.RemoveAt(0);
            pq2.RemoveAt(0);
        }
        else
        {
            if (pq2.Count == 0)
            {
                break;
            }
            pq2.RemoveAt(0);
        }
    }
    return c;
}
 
// Driver code
static public void Main()
{
    int[] A = { 10, 3, 7, 5, 8 };
    int[] B = { 8, 6, 2, 5, 9 };
    int N = A.Length;
     
    Console.WriteLine(check(A, B, N));
}
}
 
// This code is contributed by avanitrachhadiya2155


输出:
4

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

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