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 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
// Java implementation of the above approachimport 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 |
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 |
C#
// C# implementation of the above approachusing 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 |
Javascript
<script>Â
// javascript implementation of the above approach// Recursive function to find // gcd using euclidean algorithm function gcd(a, b) { Â Â Â Â if (a == 0) Â Â Â Â Â Â Â Â return b; Â Â Â Â return gcd(b % a, a); } Â
// Function to find lcm // of two numbers using gcd function 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)))
Auxiliary Space: O(log(min(n, m)))
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



