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

15 Mar, 2021
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 elementInput :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++
Java
Python3
C#
Javascript
// C++ implementation of the above approach#include <bits/stdc++.h> using namespace std; // Recursive function to find// gcd using euclidean algorithmint gcd(int a, int b){    if (a == 0)        return b;    return gcd(b % a, a);} // Function to find lcm// of two numbers using gcdint lcm(int n, int m){    return (n * m) / gcd(n, m);} // Driver codeint main(){    int n = 2, m = 3, k = 5;     cout << k / lcm(n, m) << endl;     return 0;}
// Java implementation of the above approachimport java.util.*;import java.lang.*;import java.io.*; class GFG{ // Recursive function to find// gcd using euclidean algorithmstatic int gcd(int a, int b){    if (a == 0)        return b;    return gcd(b % a, a);} // Function to find lcm// of two numbers using gcdstatic int lcm(int n, int m){    return (n * m) / gcd(n, m);} // Driver codepublic 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
# Python3 implementation of the above approach # Recursive function to find# gcd using euclidean algorithmdef gcd(a, b) :     if (a == 0) :        return b;             return gcd(b % a, a); # Function to find lcm# of two numbers using gcddef lcm(n, m) :     return (n * m) // gcd(n, m);  # Driver codeif __name__ == "__main__" :     n = 2; m = 3; k = 5;     print(k // lcm(n, m)); # This code is contributed by AnkitRai01
// C# implementation of the above approachusing System;     class GFG{ // Recursive function to find// gcd using euclidean algorithmstatic int gcd(int a, int b){    if (a == 0)        return b;    return gcd(b % a, a);} // Function to find lcm// of two numbers using gcdstatic int lcm(int n, int m){    return (n * m) / gcd(n, m);} // Driver codepublic 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
<script> // javascript implementation of the above approach// Recursive function to find// gcd using euclidean algorithmfunction gcd(a, b){    if (a == 0)        return b;    return gcd(b % a, a);} // Function to find lcm// of two numbers using gcdfunction lcm(n, m){    return (n * m) / gcd(n, m);} // Driver code var n = 2, m = 3, k = 5; document.write( parseInt(k / lcm(n, m))); // This code is contributed by Amit Katiyar </script>
0
 
Time Complexity : O(log(min(n,m))) 
ankthon
mohit kumar 29
princi singh
amit143katiyar
GCD-LCM
Competitive Programming
Greedy
Mathematical
Greedy
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Modulo 10^9+7 (1000000007)
Bits manipulation (Important tactics)
Prefix Sum Array - Implementation and Applications in Competitive Programming
Top 15 Websites for Coding Challenges and Competitions
Formatted output in Java
Dijkstra's shortest path algorithm | Greedy Algo-7
Kruskal’s Minimum Spanning Tree Algorithm | Greedy Algo-2
Prim’s Minimum Spanning Tree (MST) | Greedy Algo-5
Program for array rotation
Huffman Coding | Greedy Algo-3