# Count common elements in two arrays containing multiples of N and M

Given two arrays such that the first array contains multiples of an integer n which are less than or equal to k and similarly, the second array contains multiples of an integer m which are less than or equal to k.

The task is to find the number of common elements between the arrays.

**Examples:**

Input :n=2 m=3 k=9

Output :1

First array would be = [ 2, 4, 6, 8 ]

Second array would be = [ 3, 6, 9 ]

6 is the only common element

Input :n=1 m=2 k=5

Output :2

**Approach :**

Find the **LCM** of n and m .As LCM is the least common multiple of n and m, all the multiples of LCM would be common in both the arrays. The number of multiples of LCM which are less than or equal to k would be equal to k/(LCM(m, n)).

To find the LCM first calculate the GCD of two numbers using the Euclidean algorithm and lcm of n, m is n*m/gcd(n, m).

Below is the implementation of the above approach:

## C++

`// C++ implementation of the above approach ` `#include <bits/stdc++.h> ` ` ` `using` `namespace` `std; ` ` ` `// Recursive function to find ` `// gcd using euclidean algorithm ` `int` `gcd(` `int` `a, ` `int` `b) ` `{ ` ` ` `if` `(a == 0) ` ` ` `return` `b; ` ` ` `return` `gcd(b % a, a); ` `} ` ` ` `// Function to find lcm ` `// of two numbers using gcd ` `int` `lcm(` `int` `n, ` `int` `m) ` `{ ` ` ` `return` `(n * m) / gcd(n, m); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 2, m = 3, k = 5; ` ` ` ` ` `cout << k / lcm(n, m) << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the above approach ` `import` `java.util.*; ` `import` `java.lang.*; ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Recursive function to find ` `// gcd using euclidean algorithm ` `static` `int` `gcd(` `int` `a, ` `int` `b) ` `{ ` ` ` `if` `(a == ` `0` `) ` ` ` `return` `b; ` ` ` `return` `gcd(b % a, a); ` `} ` ` ` `// Function to find lcm ` `// of two numbers using gcd ` `static` `int` `lcm(` `int` `n, ` `int` `m) ` `{ ` ` ` `return` `(n * m) / gcd(n, m); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `2` `, m = ` `3` `, k = ` `5` `; ` ` ` ` ` `System.out.print( k / lcm(n, m)); ` `} ` `} ` ` ` `// This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the above approach ` ` ` `# Recursive function to find ` `# gcd using euclidean algorithm ` `def` `gcd(a, b) : ` ` ` ` ` `if` `(a ` `=` `=` `0` `) : ` ` ` `return` `b; ` ` ` ` ` `return` `gcd(b ` `%` `a, a); ` ` ` `# Function to find lcm ` `# of two numbers using gcd ` `def` `lcm(n, m) : ` ` ` ` ` `return` `(n ` `*` `m) ` `/` `/` `gcd(n, m); ` ` ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `n ` `=` `2` `; m ` `=` `3` `; k ` `=` `5` `; ` ` ` ` ` `print` `(k ` `/` `/` `lcm(n, m)); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the above approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Recursive function to find ` `// gcd using euclidean algorithm ` `static` `int` `gcd(` `int` `a, ` `int` `b) ` `{ ` ` ` `if` `(a == 0) ` ` ` `return` `b; ` ` ` `return` `gcd(b % a, a); ` `} ` ` ` `// Function to find lcm ` `// of two numbers using gcd ` `static` `int` `lcm(` `int` `n, ` `int` `m) ` `{ ` ` ` `return` `(n * m) / gcd(n, m); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `n = 2, m = 3, k = 5; ` ` ` ` ` `Console.WriteLine( k / lcm(n, m)); ` `} ` `} ` ` ` `// This code is contributed by Princi Singh ` |

*chevron_right*

*filter_none*

**Output:**

0

** Time Complexity : ** O(log(min(n,m)))

## Recommended Posts:

- Count of common multiples of two numbers in a range
- Maximize count of corresponding same elements in given Arrays by Rotation
- Count arrays of length K whose product of elements is same as that of given array
- Given a number n, count all multiples of 3 and/or 5 in set {1, 2, 3, ... n}
- Count of Multiples of A ,B or C less than or equal to N
- Sum of multiples of Array elements within a given range [L, R]
- Count of multiples in an Array before every element
- Sort elements of the array that occurs in between multiples of K
- Minimum sum after subtracting multiples of k from the elements of the array
- Count of possible arrays from prefix-sum and suffix-sum arrays
- Count subarrays such that remainder after dividing sum of elements by K gives count of elements
- Count divisors of n that have at-least one digit common with n
- Count common characters in two strings
- Count of common subarrays in two different permutations of 1 to N
- Maximum count of common divisors of A and B such that all are co-primes to one another
- Count common prime factors of two numbers
- Count of Array elements to be divided by 2 to make at least K elements equal
- Minimum elements to be added in a range so that count of elements is divisible by K
- Count of array elements which is smaller than both its adjacent elements
- Count of elements whose absolute difference with the sum of all the other elements is greater than k

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.