Count number of common elements between a sorted array and a reverse sorted array
Last Updated :
22 Mar, 2023
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 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
Naive Approach:- Check for all elements in array A that is present in array B or not if Yes increase the count of pair.
Implementation:-
C++
#include <bits/stdc++.h>
using namespace std;
int countEqual( int A[], int B[], int N)
{
int ans=0;
for ( int i=0;i<N;i++)
{
for ( int j=0;j<N;j++)
{
if (A[i]==B[j])
{
ans++;
break ;
}
}
}
return ans;
}
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
import java.io.*;
class GFG
{
public static int countEqual( int [] A, int [] B, int N)
{
int ans = 0 ;
for ( int i = 0 ;i < N;i++)
{
for ( int j = 0 ;j < N;j++)
{
if (A[i] == B[j])
{
ans++;
break ;
}
}
}
return ans;
}
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.print(countEqual(A, B, N));
}
}
|
Javascript
function countEqual(A, B, N)
{
let ans = 0;
for (let i = 0; i < N; i++)
{
for (let j = 0; j < N; j++)
{
if (A[i] == B[j]) {
ans++;
break ;
}
}
}
return ans;
}
let A = [ 2, 4, 5, 8, 12, 13, 17, 18, 20, 22, 309, 999 ];
let B = [ 109, 99, 68, 54, 22, 19, 17, 13, 11, 5, 3, 1 ];
let N = A.length;
console.log(countEqual(A, B, N));
|
C#
using System;
class GFG
{
public static int countEqual( int [] A, int [] B, int N)
{
int ans = 0;
for ( int i = 0;i < N;i++)
{
for ( int j = 0;j < N;j++)
{
if (A[i] == B[j])
{
ans++;
break ;
}
}
}
return ans;
}
public 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));
}
}
|
Python3
def countEqual(A, B, N):
ans = 0
for i in range (N):
for j in range (N):
if A[i] = = B[j]:
ans + = 1
break
return ans
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))
|
Time Complexity:- O(N^2)
Auxiliary Space:- O(1)
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++
#include <bits/stdc++.h>
using namespace std;
int countEqual( int A[], int B[], int N)
{
int first = 0;
int second = N - 1;
int count = 0;
while (first < N && second >= 0) {
if (A[first] < B[second]) {
first++;
}
else if (B[second] < A[first]) {
second--;
}
else {
count++;
first++;
second--;
}
}
return count;
}
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
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG
{
static int countEqual( int A[], int B[], int N)
{
int first = 0 ;
int second = N - 1 ;
int count = 0 ;
while (first < N && second >= 0 ) {
if (A[first] < B[second]) {
first++;
}
else if (B[second] < A[first]) {
second--;
}
else {
count++;
first++;
second--;
}
}
return count;
}
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));
}
}
|
Python3
def countEqual(A, B, N) :
first = 0
second = N - 1
count = 0
while (first < N and second > = 0 ) :
if (A[first] < B[second]) :
first + = 1
elif (B[second] < A[first]) :
second - = 1
else :
count + = 1
first + = 1
second - = 1
return count
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))
|
C#
using System;
class GFG{
static int countEqual( int [] A, int [] B, int N)
{
int first = 0;
int second = N - 1;
int count = 0;
while (first < N && second >= 0)
{
if (A[first] < B[second])
{
first++;
}
else if (B[second] < A[first])
{
second--;
}
else
{
count++;
first++;
second--;
}
}
return count;
}
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));
}
}
|
Javascript
<script>
function countEqual(A, B, N)
{
let first = 0;
let second = N - 1;
let count = 0;
while (first < N && second >= 0) {
if (A[first] < B[second]) {
first++;
}
else if (B[second] < A[first]) {
second--;
}
else {
count++;
first++;
second--;
}
}
return count;
}
let A = [2, 4, 5, 8, 12, 13, 17,
18, 20, 22, 309, 999];
let B = [109, 99, 68, 54, 22, 19,
17, 13, 11, 5, 3, 1];
let N = A.length;
document.write(countEqual(A, B, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Another Approach : We will use Binary search to check if the element of array B[] is present in the array A[] or not because array A[] is already sorted in increasing order. So , we can use binary search for finding elements.
Below is the implementation of the above approach :
C++
#include <bits/stdc++.h>
using namespace std;
bool binarysearch( int arr[], int N, int x)
{
int l = 0, r = N - 1;
while (l <= r) {
int mid = (l + r) / 2;
if (arr[mid] == x) {
return true ;
}
else if (arr[mid] < x) {
l = mid + 1;
}
else {
r = mid - 1;
}
}
return false ;
}
int countEqual( int A[], int B[], int N, int M)
{ int count = 0;
for ( int i = 0; i < M; i++)
{
if (binarysearch(A, N, B[i]))
{
count++;
}
}
return count;
}
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 );
int M = sizeof (B) / sizeof ( int );
cout << countEqual(A, B, N, M)<<endl;
return 0;
}
|
Java
import java.util.Arrays;
class Main {
static boolean binarySearch( int [] arr, int N, int x) {
int l = 0 , r = N - 1 ;
while (l <= r) {
int mid = (l + r) / 2 ;
if (arr[mid] == x) {
return true ;
} else if (arr[mid] < x) {
l = mid + 1 ;
} else {
r = mid - 1 ;
}
}
return false ;
}
static int countEqual( int [] A, int [] B, int N, int M) {
int count = 0 ;
Arrays.sort(A);
for ( int i = 0 ; i < M; i++) {
if (binarySearch(A, N, B[i])) {
count++;
}
}
return count;
}
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;
int M = B.length;
System.out.println(countEqual(A, B, N, M));
}
}
|
Python3
def binarySearch(arr, N, x):
l = 0
r = N - 1
while l < = r:
mid = (l + r) / / 2
if arr[mid] = = x:
return True
elif arr[mid] < x:
l = mid + 1
else :
r = mid - 1
return False
def countEqual(A, B, N, M):
count = 0
A.sort()
for i in range (M):
if binarySearch(A, N, B[i]):
count + = 1
return count
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)
M = len (B)
print (countEqual(A, B, N, M))
|
C#
using System;
class Program
{
static bool BinarySearch( int [] arr, int N, int x)
{
int l = 0, r = N - 1;
while (l <= r) {
int mid = (l + r) / 2;
if (arr[mid] == x) {
return true ;
}
else if (arr[mid] < x) {
l = mid + 1;
}
else {
r = mid - 1;
}
}
return false ;
}
static int CountEqual( int [] A, int [] B, int N, int M)
{
int count = 0;
for ( int i = 0; i < M; i++) {
if (BinarySearch(A, N, B[i])) {
count++;
}
}
return count;
}
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;
int M = B.Length;
Console.WriteLine(CountEqual(A, B, N, M));
Console.ReadLine();
}
}
|
Javascript
function binarysearch(arr, N, x) {
let l = 0, r = N - 1;
while (l <= r) {
let mid = Math.floor((l + r) / 2);
if (arr[mid] === x) {
return true ;
}
else if (arr[mid] < x) {
l = mid + 1;
}
else {
r = mid - 1;
}
}
return false ;
}
function countEqual(A, B, N, M) {
let count = 0;
for (let i = 0; i < M; i++) {
if (binarysearch(A, N, B[i])) {
count++;
}
}
return count;
}
(() => {
const A = [2, 4, 5, 8, 12, 13, 17, 18, 20, 22, 309, 999];
const B = [109, 99, 68, 54, 22, 19, 17, 13, 11, 5, 3, 1];
const N = A.length;
const M = B.length;
console.log(countEqual(A, B, N, M));
})();
|
Time Complexity: O(M*log(N))
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...