Skip to content
Related Articles
Count number of common elements between a sorted array and a reverse sorted array
• Last Updated : 26 May, 2021

Given two arrays consisting of N distinct integers such that the array A[] and B[] are sorted in ascending and descending order respectively, the task is to find the number of values common in both the arrays.

Examples:

Input: A[] = {1, 10, 100}, B[] = {200, 20, 2}
Output: 0

Input: 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}
Output: 4

Approach: The given problem can be solved by using the Two Pointer Approach. Follow the steps below to solve the problem:

• Initialize two variables, say first as 0 and second as (N – 1) that is used to traverse the array A[] and B[] from the front and back respectively.
• Initialize a variable, say count as 0 that stores the count of numbers common in the array A[] and B[].
• Iterate a loop until first < N and second >= 0 and perform the following steps:
• If the value of A[first] is equal to B[second], then increment the values of count and first and decrement the value of the second.
• If the value of A[first] is less than B[second], then increment the value of first.
• If the value of A[first] is greater than B[second], then decrement the value of the second.
• After completing the above steps, print the value of count as the result.

Below is the implementation of the above approach:

## 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

 ``
Output:
`4`

Time Complexity: O(N)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer DSA Live Classes

My Personal Notes arrow_drop_up