Given two arrays arr1[] and arr2[] of size M and N respectively. Both arrays are in Arithmetic Progression and the first element of both arrays is the same. The task is to find the number of common elements in arr1[] and arr2[].
Examples:
Input: arr1[] = {2, 3, 4, 5, 6}, arr2[] = {2, 4, 6, 8, 10}
Output: 3
Explanation:
Common elements are {2, 4, 6}
Input: arr1[] = {1, 4, 7, 10, 13, 16}, arr2[] = {1, 3, 5, 7, 9}
Output: 2
Explanation:
Common elements are {1, 7}
Approach: The idea is to use Least Common Multiple of the common difference of the two Arithmetic Progression to solve this problem. Below is the illustration of the steps:
- Find the common difference of the two arithmetic progression with the help of the below formulae
diff1 = arr1[1] - arr1[0]
diff2 = arr2[1] - arr2[0]
- Find the Least common multiple of the common difference of the two arithmetic progression.
- Common elements that are possible in the two arithmetic progression will be the difference of the last elements of the arithmetic progression to the first element divided by the LCM of the common difference.
elements1 = (arr1[m-1] - arr1[0]) / LCM(diff1, diff2)
elements2 = (arr2[n-1] - arr2[0]) / LCM(diff1, diff2)
// Common Elements
ans = min(elements, elements2)
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
int findlcm( int a, int b)
{
int gc = gcd(a, b);
return a * b / gc;
}
int CountCommon( int arr1[], int arr2[],
int m, int n)
{
int diff1 = arr1[1] - arr1[0];
int diff2 = arr2[1] - arr2[0];
int lcm = findlcm(diff1, diff2);
int ans1 = (arr1[m - 1] - arr1[0]) / lcm;
int ans2 = (arr2[n - 1] - arr2[0]) / lcm;
int ans = min(ans1, ans2);
return (ans + 1);
}
int main()
{
int arr1[] = { 2, 5, 8, 11, 14, 17 };
int arr2[] = { 2, 4, 6, 8, 10, 12 };
int m = sizeof (arr1) / sizeof (arr1[0]);
int n = sizeof (arr2) / sizeof (arr2[0]);
cout << CountCommon(arr1, arr2, m, n);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int gcd( int a, int b)
{
if (b == 0 )
return a;
return gcd(b, a % b);
}
static int findlcm( int a, int b)
{
int gc = gcd(a, b);
return a * b / gc;
}
static int CountCommon( int []arr1,
int []arr2,
int m, int n)
{
int diff1 = arr1[ 1 ] - arr1[ 0 ];
int diff2 = arr2[ 1 ] - arr2[ 0 ];
int lcm = findlcm(diff1, diff2);
int ans1 = (arr1[m - 1 ] - arr1[ 0 ]) / lcm;
int ans2 = (arr2[n - 1 ] - arr2[ 0 ]) / lcm;
int ans = Math.min(ans1, ans2);
return (ans + 1 );
}
public static void main(String args[])
{
int []arr1 = { 2 , 5 , 8 , 11 , 14 , 17 };
int []arr2 = { 2 , 4 , 6 , 8 , 10 , 12 };
int m = arr1.length;
int n = arr2.length;
System.out.print(CountCommon(arr1, arr2, m, n));
}
}
|
Python3
def gcd(a, b):
if b = = 0 :
return a
return gcd(b, a % b)
def findlcm(a, b):
return a * b / / gcd(a, b)
def CountCommon(arr1, arr2, m, n):
diff1 = arr1[ 1 ] - arr1[ 0 ]
diff2 = arr2[ 1 ] - arr2[ 0 ]
lcm = findlcm(diff1, diff2)
ans1 = (arr1[m - 1 ] - arr1[ 0 ]) / / lcm
ans2 = (arr2[n - 1 ] - arr2[ 0 ]) / / lcm
ans = min (ans1, ans2)
print (ans + 1 )
if __name__ = = "__main__" :
arr1 = [ 2 , 5 , 8 , 11 , 14 , 17 ]
arr2 = [ 2 , 4 , 6 , 8 , 10 , 12 ]
m = len (arr1)
n = len (arr2)
CountCommon(arr1, arr2, m, n)
|
C#
using System;
class GFG{
static int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static int findlcm( int a, int b)
{
int gc = gcd(a, b);
return a * b / gc;
}
int CountCommon( int []arr1,
int []arr2,
int m, int n)
{
int diff1 = arr1[1] - arr1[0];
int diff2 = arr2[1] - arr2[0];
int lcm = findlcm(diff1, diff2);
int ans1 = (arr1[m - 1] - arr1[0]) / lcm;
int ans2 = (arr2[n - 1] - arr2[0]) / lcm;
int ans = min(ans1, ans2);
return (ans + 1);
}
public static void Main()
{
int []arr1 = { 2, 5, 8, 11, 14, 17 };
int []arr2 = { 2, 4, 6, 8, 10, 12 };
int m = arr1.Length;
int n = arr2.Length;
Console.Write(CountCommon(arr1, arr2, m, n));
}
}
|
Javascript
<script>
function gcd(a, b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
function findlcm(a, b)
{
let gc = gcd(a, b);
return a * b / gc;
}
function CountCommon(arr1, arr2,
m, n)
{
let diff1 = arr1[1] - arr1[0];
let diff2 = arr2[1] - arr2[0];
let lcm = findlcm(diff1, diff2);
let ans1 = Math.floor((arr1[m - 1] - arr1[0]) / lcm);
let ans2 = Math.floor((arr2[n - 1] - arr2[0]) / lcm);
let ans = Math.min(ans1, ans2);
return (ans + 1);
}
let arr1 = [ 2, 5, 8, 11, 14, 17 ];
let arr2 = [ 2, 4, 6, 8, 10, 12 ];
let m = arr1.length;
let n = arr2.length;
document.write(CountCommon(arr1, arr2, m, n));
</script>
|
Time Complexity:O(log(max(diff1, diff2)))
Auxiliary Space: O(log(max(diff1, diff2)))
Another Approach: We can Binary search to check if the element of first array is present in the second array or not because first and second array is already sorted in increasing order. So , we will use binary search for finding common elements.
Below is the implementation of the above approach :
C++
#include <bits/stdc++.h>
using namespace std;
bool binarysearch( int arr[], int M, int x)
{
int l = 0, r = M - 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 CountCommon( int A[], int B[], int N, int M)
{ int count=0;
for ( int i = 0; i < N; i++)
{
if (binarysearch(B, M, A[i]))
{
count++;
}
}
return count;
}
int main()
{
int A[] = { 2, 5, 8, 11, 14, 17 };
int B[] = {2, 4, 6, 8, 10, 12};
int N = sizeof (A) / sizeof ( int );
int M = sizeof (B) / sizeof ( int );
cout<< CountCommon(A, B, N, M)<< "\n" ;
return 0;
}
|
Java
import java.util.*;
public class Main {
public static boolean binarysearch( int arr[], int M,
int x)
{
int l = 0 , r = M - 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 ;
}
public static int CountCommon( int A[], int B[], int N,
int M)
{
int count = 0 ;
for ( int i = 0 ; i < N; i++)
{
if (binarysearch(B, M, A[i])) {
count++;
}
}
return count;
}
public static void main(String[] args)
{
int A[] = { 2 , 5 , 8 , 11 , 14 , 17 };
int B[] = { 2 , 4 , 6 , 8 , 10 , 12 };
int N = A.length;
int M = B.length;
System.out.println(CountCommon(A, B, N, M));
}
}
|
Python3
from typing import List
def binarysearch(arr: List [ int ], M: int , x: int ) - > bool :
l, r = 0 , M - 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 count_common(A: List [ int ], B: List [ int ], N: int , M: int ) - > int :
count = 0
for i in range (N):
if binarysearch(B, M, A[i]):
count + = 1
return count
if __name__ = = '__main__' :
A = [ 2 , 5 , 8 , 11 , 14 , 17 ]
B = [ 2 , 4 , 6 , 8 , 10 , 12 ]
N = len (A)
M = len (B)
print (count_common(A, B, N, M))
|
C#
using System;
namespace CountCommonElements {
class Program {
static bool BinarySearch( int [] arr, int M, int x)
{
int l = 0, r = M - 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 CountCommon( int [] A, int [] B, int N, int M)
{
int count = 0;
for ( int i = 0; i < N; i++) {
if (BinarySearch(B, M, A[i])) {
count++;
}
}
return count;
}
static void Main( string [] args)
{
int [] A = { 2, 5, 8, 11, 14, 17 };
int [] B = { 2, 4, 6, 8, 10, 12 };
int N = A.Length;
int M = B.Length;
Console.WriteLine(CountCommon(A, B, N, M));
}
}
}
|
Javascript
function binarysearch(arr, M, x) {
let l = 0, r = M - 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 CountCommon(A, B, N, M) {
let count = 0;
for (let i = 0; i < N; i++) {
if (binarysearch(B, M, A[i])) {
count++;
}
}
return count;
}
let A = [2, 5, 8, 11, 14, 17];
let B = [2, 4, 6, 8, 10, 12];
let N = A.length;
let M = B.length;
console.log(CountCommon(A, B, N, M));
|
Time Complexity:O(N*logM)
Auxiliary Space: O(1)
Another approach using sets –
In this approach we will use the set data structure to find the common elements between those two lists. Firstly we will convert those two lists into sets and then find the intersection between them and then count the number of elements returned by that intersection and print it.
C++
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;
int count_common_ele(vector< int > arr1, vector< int > arr2) {
unordered_set< int > set_arr1(arr1.begin(), arr1.end());
unordered_set< int > set_arr2(arr2.begin(), arr2.end());
int count1 = 0;
for ( auto num : set_arr1) {
if (set_arr2.count(num)) {
count1++;
}
}
cout << count1 << endl;
return 0;
}
int main() {
vector< int > arr1 = {2, 5, 8, 11, 14, 17};
vector< int > arr2 = {2, 4, 6, 8, 10, 12};
count_common_ele(arr1, arr2);
return 0;
}
|
Java
import java.util.HashSet;
import java.util.Set;
public class Main {
public static int countCommonElements( int [] arr1,
int [] arr2)
{
Set<Integer> set1 = new HashSet<Integer>();
Set<Integer> set2 = new HashSet<Integer>();
for ( int num : arr1) {
set1.add(num);
}
for ( int num : arr2) {
set2.add(num);
}
int count = 0 ;
for ( int num : set1) {
if (set2.contains(num)) {
count++;
}
}
System.out.println(
count);
return 0 ;
}
public static void main(String[] args)
{
int [] arr1 = { 2 , 5 , 8 , 11 , 14 , 17 };
int [] arr2 = { 2 , 4 , 6 , 8 , 10 , 12 };
countCommonElements(arr1, arr2);
}
}
|
Python3
def count_common_ele(arr1, arr2):
set_arr1 = set (arr1)
set_arr2 = set (arr2)
print ( len (set_arr1.intersection(set_arr2)))
arr1 = [ 2 , 5 , 8 , 11 , 14 , 17 ]
arr2 = [ 2 , 4 , 6 , 8 , 10 , 12 ]
count_common_ele(arr1, arr2)
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static int CountCommonElements(List< int > arr1,
List< int > arr2)
{
HashSet< int > setArr1 = new HashSet< int >(arr1);
HashSet< int > setArr2 = new HashSet< int >(arr2);
int count = setArr1.Intersect(setArr2).Count();
Console.WriteLine(
count);
return 0;
}
static void Main( string [] args)
{
List< int > arr1
= new List< int >{ 2, 5, 8, 11, 14, 17 };
List< int > arr2
= new List< int >{ 2, 4, 6, 8, 10, 12 };
CountCommonElements(arr1, arr2);
Console.ReadLine();
}
}
|
Javascript
function countCommonElements(arr1, arr2) {
const setArr1 = new Set(arr1);
const setArr2 = new Set(arr2);
console.log([...setArr1].filter(x => setArr2.has(x)).length);
}
const arr1 = [2, 5, 8, 11, 14, 17];
const arr2 = [2, 4, 6, 8, 10, 12];
countCommonElements(arr1, arr2);
|
Time Complexity – O(N + M) # Where N and M are the sizes of the arrays
Space Complexity – O(N) # converted the list into sets, N denotes the length of the list
Please Login to comment...