📌  相关文章
📜  来自给定两个数组的相等值对的计数,使得 a[i] 等于 b[j]

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

来自给定两个数组的相等值对的计数,使得 a[i] 等于 b[j]

给定两个长度分别为NM的数组a[]b[] ,按非递减顺序排序。任务是找到对(i, j)的数量,使得a[i]等于b[j]

例子:

方法:这个问题可以通过使用双指针方法来解决。让i指向数组a[]的第一个元素, j指向数组b[]的第一个元素。在遍历数组时,会有 3 种情况。

请按照以下步骤解决给定的问题。

  • 将变量ans、ij初始化为0。
  • 将答案ij初始化为0并开始遍历两个数组,直到i小于Nj小于M
    • 如果a[i]等于b[j],则计算cnt1cnt2并将答案增加cnt1 * cnt2
    • 如果a[i]小于b[j],则增加i
    • 如果a[i]大于b[j],则增加j
  • 执行上述步骤后,打印ans的值作为答案。

下面是上述方法的实现:

C++
// C++ Program for above approach
#include 
using namespace std;
 
// Function to find number of pairs with
// satisfying the given condition
int findPairs(int* a, int* b, int n, int m)
{
 
    // Initialize ans, i, j to 0 .
    int ans = 0, i = 0, j = 0;
 
    // Use the two pointer approach to
    // calculate the answer .
    while (i < n && j < m) {
 
        // Case - 1
        if (a[i] == b[j]) {
 
            // Target denotes a[i]
            // or b[j] as a[i] = b[j].
 
            // cnt1 denotes the number
            // of elements in array
            // a that are equal to target.
 
            // cnt2 denotes the number
            // of elements in array
            // b that are equal to target
            int target = a[i], cnt1 = 0, cnt2 = 0;
 
            // Calculate cnt1
            while (i < n && a[i] == target) {
                cnt1++;
                i++;
            }
 
            // Calculate cnt2
            while (j < m && b[j] == target) {
                cnt2++;
                j++;
            }
 
            // Increment the answer by (cnt1 * cnt2)
            ans += (cnt1 * cnt2);
        }
 
        // Case - 2
        else if (a[i] < b[j])
            i++;
 
        // Case - 3
        else
            j++;
    }
 
    // Return the answer
    return ans;
}
 
// Driver Code
int main()
{
    int N = 8, M = 7;
    int a[] = { 1, 1, 3, 3, 3, 5, 8, 8 };
    int b[] = { 1, 3, 3, 4, 5, 5, 5 };
 
    cout << findPairs(a, b, N, M);
}


Java
// Java program for above approach
import java.io.*;
 
class GFG{
 
// Function to find number of pairs with
// satisfying the given condition
static int findPairs(int[] a, int[] b, int n, int m)
{
     
    // Initialize ans, i, j to 0 .
    int ans = 0, i = 0, j = 0;
 
    // Use the two pointer approach to
    // calculate the answer .
    while (i < n && j < m)
    {
         
        // Case - 1
        if (a[i] == b[j])
        {
             
            // Target denotes a[i]
            // or b[j] as a[i] = b[j].
 
            // cnt1 denotes the number
            // of elements in array
            // a that are equal to target.
 
            // cnt2 denotes the number
            // of elements in array
            // b that are equal to target
            int target = a[i], cnt1 = 0, cnt2 = 0;
 
            // Calculate cnt1
            while (i < n && a[i] == target)
            {
                cnt1++;
                i++;
            }
             
            // Calculate cnt2
            while (j < m && b[j] == target)
            {
                cnt2++;
                j++;
            }
 
            // Increment the answer by (cnt1 * cnt2)
            ans += (cnt1 * cnt2);
        }
 
        // Case - 2
        else if (a[i] < b[j])
            i++;
 
        // Case - 3
        else
            j++;
    }
 
    // Return the answer
    return ans;
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 8, M = 7;
    int a[] = { 1, 1, 3, 3, 3, 5, 8, 8 };
    int b[] = { 1, 3, 3, 4, 5, 5, 5 };
 
    System.out.println(findPairs(a, b, N, M));
}
}
 
// This code is contributed by Potta Lokesh


Python3
# Python3 program for above approach
 
# Function to find number of pairs with
# satisfying the given condition
def findPairs(a, b, n, m):
     
    # Initialize ans, i, j to 0 .
    ans = 0
    i = 0
    j = 0
 
    # Use the two pointer approach to
    # calculate the answer .
    while (i < n and j < m):
 
        # Case - 1
        if (a[i] == b[j]):
 
            # Target denotes a[i]
            # or b[j] as a[i] = b[j].
 
            # cnt1 denotes the number
            # of elements in array
            # a that are equal to target.
 
            # cnt2 denotes the number
            # of elements in array
            # b that are equal to target
            target = a[i]
            cnt1 = 0
            cnt2 = 0
 
            # Calculate cnt1
            while (i < n and a[i] == target):
                cnt1 += 1
                i += 1
 
            # Calculate cnt2
            while (j < m and b[j] == target):
                cnt2 += 1
                j += 1
 
            # Increment the answer by (cnt1 * cnt2)
            ans += (cnt1 * cnt2)
 
        # Case - 2
        elif (a[i] < b[j]):
            i += 1
 
        # Case- 3
        else:
            j += 1
 
    # Return the answer
    return ans
 
# Driver Code
if __name__ == "__main__":
 
    N = 8
    M = 7
    a = [ 1, 1, 3, 3, 3, 5, 8, 8 ]
    b = [ 1, 3, 3, 4, 5, 5, 5 ]
 
    print(findPairs(a, b, N, M))
 
# This code is contributed by ukasp


C#
// C# program for above approach
using System;
 
class GFG{
 
// Function to find number of pairs with
// satisfying the given condition
static int findPairs(int[] a, int[] b, int n, int m)
{
     
    // Initialize ans, i, j to 0 .
    int ans = 0, i = 0, j = 0;
 
    // Use the two pointer approach to
    // calculate the answer .
    while (i < n && j < m)
    {
         
        // Case - 1
        if (a[i] == b[j])
        {
             
            // Target denotes a[i]
            // or b[j] as a[i] = b[j].
 
            // cnt1 denotes the number
            // of elements in array
            // a that are equal to target.
 
            // cnt2 denotes the number
            // of elements in array
            // b that are equal to target
            int target = a[i], cnt1 = 0, cnt2 = 0;
 
            // Calculate cnt1
            while (i < n && a[i] == target)
            {
                cnt1++;
                i++;
            }
             
            // Calculate cnt2
            while (j < m && b[j] == target)
            {
                cnt2++;
                j++;
            }
 
            // Increment the answer by (cnt1 * cnt2)
            ans += (cnt1 * cnt2);
        }
 
        // Case - 2
        else if (a[i] < b[j])
            i++;
 
        // Case - 3
        else
            j++;
    }
 
    // Return the answer
    return ans;
}
 
// Driver Code
public static void Main()
{
    int N = 8, M = 7;
    int []a = { 1, 1, 3, 3, 3, 5, 8, 8 };
    int []b = { 1, 3, 3, 4, 5, 5, 5 };
 
    Console.Write(findPairs(a, b, N, M));
}
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript


输出
11

时间复杂度: O(N + M)
辅助空间: O(1)