📌  相关文章
📜  不同可能对的计数,使得 A 中的元素大于 B 中的元素

📅  最后修改于: 2021-09-07 02:22:01             🧑  作者: Mango

给定两个等长的给定数组AB ,任务是找到可以选择的不同元素对的最大数量,使得 A 中的元素严格大于 B 中的元素。

例子:

方法:为了解决这个问题,我们需要采用以下方法:

  • 对两个数组进行排序。
  • 遍历数组 A 的整个长度,并检查 A 中的当前元素是否大于 B 中当前指向 B 的元素。
  • 如果是,则指向两个数组中的下一个元素。否则,移动到 A 中的下一个元素并检查它是否大于当前指向 B 的元素。
  • 数组 A 的整个遍历之后在数组 B 中遍历的元素数给出了所需的答案。

下面是上述方法的实现:

C++
// C++ Program to count number of distinct
// pairs possible from the two arrays
// such that element selected from one array is
// always greater than the one selected from
// the other array
 
#include 
using namespace std;
 
// Function to return the count
// of pairs
int countPairs(vector A,
                    vector B)
{
    int n = A.size();
     
    sort(A.begin(),A.end());
    sort(B.begin(),B.end());
     
    int ans = 0, i;
    for (int i = 0; i < n; i++) {
        if (A[i] > B[ans]) {
            ans++;
        }
    }
     
    return ans;
}
 
// Driver code
int main()
{
    vector A = { 30, 28, 45, 22 };
    vector B = { 35, 25, 22, 48 };
     
    cout << countPairs(A,B);
     
    return 0;
}


Java
// Java program to count number of distinct
// pairs possible from the two arrays such
// that element selected from one array is
// always greater than the one selected from
// the other array
import java.util.*;
 
class GFG{
 
// Function to return the count
// of pairs
static int countPairs(int [] A,
                      int [] B)
{
    int n = A.length;
    int ans = 0;
     
    Arrays.sort(A);
    Arrays.sort(B);
     
    for(int i = 0; i < n; i++)
    {
       if (A[i] > B[ans])
       {
           ans++;
       }
    }
    return ans;
}
 
// Driver code
public static void main(String[] args)
{
    int [] A = { 30, 28, 45, 22 };
    int [] B = { 35, 25, 22, 48 };
     
    System.out.print(countPairs(A, B));
}
}
 
// This code is contributed by sapnasingh4991


Python3
# Python3 program to count number of distinct
# pairs possible from the two arrays
# such that element selected from one array is
# always greater than the one selected from
# the other array
 
# Function to return the count
# of pairs
def countPairs(A, B):
 
    n = len(A)
 
    A.sort()
    B.sort()
 
    ans = 0
    for i in range(n):
        if(A[i] > B[ans]):
            ans += 1
 
    return ans
 
# Driver code
if __name__ == '__main__':
    A = [30, 28, 45, 22]
    B = [35, 25, 22, 48]
 
    print(countPairs(A, B))
 
# This code is contributed by Shivam Singh


C#
// C# program to count number of distinct
// pairs possible from the two arrays such
// that element selected from one array is
// always greater than the one selected from
// the other array
using System;
class GFG{
 
// Function to return the count
// of pairs
static int countPairs(int [] A,
                      int [] B)
{
    int n = A.Length;
    int ans = 0;
     
    Array.Sort(A);
    Array.Sort(B);
     
    for(int i = 0; i < n; i++)
    {
        if (A[i] > B[ans])
        {
            ans++;
        }
    }
    return ans;
}
 
// Driver code
public static void Main()
{
    int []A = { 30, 28, 45, 22 };
    int []B = { 35, 25, 22, 48 };
     
    Console.Write(countPairs(A, B));
}
}
 
// This code is contributed by Code_Mech


Javascript


输出:
3

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