📜  计算排序数组和反向排序数组之间的公共元素数

📅  最后修改于: 2021-10-26 06:28:30             🧑  作者: Mango

给定两个由N 个不同整数组成的数组,使得数组A[]B[]分别按升序和降序排序,任务是找到两个数组中共有值的数量。

例子:

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

  • 初始化两个变量,第一个0第二个(N – 1) ,用于分别从前面和后面遍历数组A[]B[]
  • 初始化一个变量,比如count0 ,它存储数组A[]B[] 中常见数字的计数。
  • 迭代一个循环,直到first < N and second >= 0并执行以下步骤:
    • 如果A[first] 的值等于B[second] ,则增加countfirst的值并减少second的值。
    • 如果A[first] 的值小于B[second] ,则增加first的值。
    • 如果A[first]的值大于B[second] ,则减少second的值。
  • 完成以上步骤后,打印count的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to count the number of
// elements common in both the arrays
int countEqual(int A[], int B[], int N)
{
    // Used to traverse array A[] and
    // B[] from the front and the back
    int first = 0;
    int second = N - 1;
 
    // Stores the count of numbers
    // common in both array
    int count = 0;
 
    while (first < N && second >= 0) {
 
        // If A[first] is less than
        // B[second]
        if (A[first] < B[second]) {
 
            // Increment the value
            // of first
            first++;
        }
 
        // IF B[second] is less
        // than A[first]
        else if (B[second] < A[first]) {
 
            // Decrement the value
            // of second
            second--;
        }
 
        // A[first] is equal to
        // B[second]
        else {
 
            // Increment the value
            // of count
            count++;
 
            // Increment the value
            // of first
            first++;
 
            // Decrement the value
            // of second
            second--;
        }
    }
 
    // Return the value of count
    return count;
}
 
// Driver Code
int main()
{
    int A[] = { 2, 4, 5, 8, 12, 13, 17,
                18, 20, 22, 309, 999 };
    int B[] = { 109, 99, 68, 54, 22, 19,
                17, 13, 11, 5, 3, 1 };
    int N = sizeof(A) / sizeof(int);
    cout << countEqual(A, B, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG
{
 
// Function to count the number of
// elements common in both the arrays
static int countEqual(int A[], int B[], int N)
{
   
    // Used to traverse array A[] and
    // B[] from the front and the back
    int first = 0;
    int second = N - 1;
   
    // Stores the count of numbers
    // common in both array
    int count = 0;
   
    while (first < N && second >= 0) {
   
        // If A[first] is less than
        // B[second]
        if (A[first] < B[second]) {
   
            // Increment the value
            // of first
            first++;
        }
   
        // IF B[second] is less
        // than A[first]
        else if (B[second] < A[first]) {
   
            // Decrement the value
            // of second
            second--;
        }
   
        // A[first] is equal to
        // B[second]
        else {
   
            // Increment the value
            // of count
            count++;
   
            // Increment the value
            // of first
            first++;
   
            // Decrement the value
            // of second
            second--;
        }
    }
   
    // Return the value of count
    return count;
}
   
    // Driver Code
    public static void main(String[] args)
    {
 
        int A[] = { 2, 4, 5, 8, 12, 13, 17,
                18, 20, 22, 309, 999 };
    int B[] = { 109, 99, 68, 54, 22, 19,
                17, 13, 11, 5, 3, 1 };
    int N = A.length;
    System.out.println(countEqual(A, B, N));
    }
}
 
// This code is contributed by susmitakundugoaldanga.


Python3
# Python program for the above approach
 
# Function to count the number of
# elements common in both the arrays
def countEqual(A, B, N) :
     
    # Used to traverse array A[] and
    # B[] from the front and the back
    first = 0
    second = N - 1
  
    # Stores the count of numbers
    # common in both array
    count = 0
  
    while (first < N and second >= 0) :
  
        # If A[first] is less than
        # B[second]
        if (A[first] < B[second]) :
  
            # Increment the value
            # of first
            first += 1
         
  
        # IF B[second] is less
        # than A[first]
        elif (B[second] < A[first]) :
  
            # Decrement the value
            # of second
            second -= 1
         
  
        # A[first] is equal to
        # B[second]
        else :
  
            # Increment the value
            # of count
            count += 1
  
            # Increment the value
            # of first
            first += 1
  
            # Decrement the value
            # of second
            second -= 1
         
    # Return the value of count
    return count
 
# Driver Code
 
A= [ 2, 4, 5, 8, 12, 13, 17,
                18, 20, 22, 309, 999 ]
B = [ 109, 99, 68, 54, 22, 19,
                17, 13, 11, 5, 3, 1 ]
N = len(A)
print(countEqual(A, B, N))
 
# This code is contributed by sanjou_62.


C#
// C# program for the above approach
using System;
 
class GFG{
     
// Function to count the number of
// elements common in both the arrays
static int countEqual(int[] A, int[] B, int N)
{
 
    // Used to traverse array A[] and
    // B[] from the front and the back
    int first = 0;
    int second = N - 1;
 
    // Stores the count of numbers
    // common in both array
    int count = 0;
 
    while (first < N && second >= 0)
    {
         
        // If A[first] is less than
        // B[second]
        if (A[first] < B[second])
        {
             
            // Increment the value
            // of first
            first++;
        }
 
        // IF B[second] is less
        // than A[first]
        else if (B[second] < A[first])
        {
 
            // Decrement the value
            // of second
            second--;
        }
 
        // A[first] is equal to
        // B[second]
        else
        {
 
            // Increment the value
            // of count
            count++;
 
            // Increment the value
            // of first
            first++;
 
            // Decrement the value
            // of second
            second--;
        }
    }
 
    // Return the value of count
    return count;
}
 
// Driver code
static void Main()
{
    int[] A = { 2, 4, 5, 8, 12, 13,
                17, 18, 20, 22, 309, 999 };
    int[] B = { 109, 99, 68, 54, 22, 19,
                17, 13, 11, 5, 3, 1 };
    int N = A.Length;
     
    Console.WriteLine(countEqual(A, B, N));
}
}
 
// This code is contributed by abhinavjain194


Javascript


输出:
4

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程