Find numbers which are multiples of first array and factors of second array

Given two arrays A[] and B[], the task is to find the integers which are divisible by all the elements of array A[] and divide all the elements of array B[].
Examples:Â Â
Input: A[] = {1, 2, 2, 4}, B[] = {16, 32, 64}Â
Output: 4 8 16Â
4, 8 and 16 are the only numbers thatÂ
are multiples of all the elements of array A[]Â
and divide all the elements of array B[]Input: A[] = {2, 3, 6}, B[] = {42, 84}Â
Output: 6 42Â
Â
Approach: If X is a multiple of all the elements of the first array then X must be a multiple of the LCM of all the elements of the first array.Â
Similarly, If X is a factor of all the elements of the second array then it must be a factor of the GCD of all the elements of the second array and such X will exist only if GCD of the second array is divisible by the LCM of the first array.Â
If it is divisible then X can be any value from the range [LCM, GCD] which is a multiple of LCM and evenly divides GCD.
Below is the implementation of above approach:Â Â
C++
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;Â
// Function to return the LCM of two numbersint lcm(int x, int y){Â Â Â Â int temp = (x * y) / __gcd(x, y);Â Â Â Â return temp;}Â
// Function to print the required numbersvoid findNumbers(int a[], int n, int b[], int m){Â
    // To store the lcm of array a[] elements    // and the gcd of array b[] elements    int lcmA = 1, gcdB = 0;Â
    // Finding LCM of first array    for (int i = 0; i < n; i++)        lcmA = lcm(lcmA, a[i]);Â
    // Finding GCD of second array    for (int i = 0; i < m; i++)        gcdB = __gcd(gcdB, b[i]);Â
    // No such element exists    if (gcdB % lcmA != 0) {        cout << "-1";        return;    }Â
    // All the multiples of lcmA which are    // less than or equal to gcdB and evenly    // divide gcdB will satisfy the conditions    int num = lcmA;    while (num <= gcdB) {        if (gcdB % num == 0)            cout << num << " ";        num += lcmA;    }}Â
// Driver codeint main(){Â
    int a[] = { 1, 2, 2, 4 };    int b[] = { 16, 32, 64 };Â
    int n = sizeof(a) / sizeof(a[0]);    int m = sizeof(b) / sizeof(b[0]);Â
    findNumbers(a, n, b, m);Â
    return 0;} |
Java
// Java implementation of the approachimport java.util.*;Â
class GFG{static int __gcd(int a, int b) { Â Â Â Â if (b == 0) Â Â Â Â Â Â Â Â return a; Â Â Â Â return __gcd(b, a % b); Â Â Â Â Â }Â
// Function to return the LCM of two numbersstatic int lcm(int x, int y){Â Â Â Â int temp = (x * y) / __gcd(x, y);Â Â Â Â return temp;}Â
// Function to print the required numbersstatic void findNumbers(int a[], int n, Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â int b[], int m){Â
    // To store the lcm of array a[] elements    // and the gcd of array b[] elements    int lcmA = 1, gcdB = 0;Â
    // Finding LCM of first array    for (int i = 0; i < n; i++)        lcmA = lcm(lcmA, a[i]);Â
    // Finding GCD of second array    for (int i = 0; i < m; i++)        gcdB = __gcd(gcdB, b[i]);Â
    // No such element exists    if (gcdB % lcmA != 0)     {        System.out.print("-1");        return;    }Â
    // All the multiples of lcmA which are    // less than or equal to gcdB and evenly    // divide gcdB will satisfy the conditions    int num = lcmA;    while (num <= gcdB)     {        if (gcdB % num == 0)            System.out.print(num + " ");        num += lcmA;    }}Â
// Driver codepublic static void main(String[] args){Â Â Â Â int a[] = { 1, 2, 2, 4 };Â Â Â Â int b[] = { 16, 32, 64 };Â
    int n = a.length;    int m = b.length;Â
    findNumbers(a, n, b, m);}}Â
// This code is contributed by 29AjayKumar |
Python3
# Python3 implementation of the approach from math import gcdÂ
# Function to return the LCM of two numbers def lcm( x, y) :Â Â Â Â Â Â Â Â Â temp = (x * y) // gcd(x, y); Â Â Â Â return temp; Â
# Function to print the required numbers def findNumbers(a, n, b, m) : Â
    # To store the lcm of array a[] elements     # and the gcd of array b[] elements     lcmA = 1; __gcdB = 0; Â
    # Finding LCM of first array     for i in range(n) :         lcmA = lcm(lcmA, a[i]); Â
    # Finding GCD of second array     for i in range(m) :         __gcdB = gcd(__gcdB, b[i]); Â
    # No such element exists     if (__gcdB % lcmA != 0) :        print("-1");         return; Â
    # All the multiples of lcmA which are     # less than or equal to gcdB and evenly     # divide gcdB will satisfy the conditions     num = lcmA;     while (num <= __gcdB) :        if (__gcdB % num == 0) :            print(num, end = " ");                      num += lcmA; Â
# Driver code if __name__ == "__main__" : Â
    a = [ 1, 2, 2, 4 ];    b = [ 16, 32, 64 ];         n = len(a);    m = len(b);         findNumbers(a, n, b, m);      # This code is contributed by AnkitRai01 |
C#
// C# implementation of the approachusing System;Â
class GFG{static int __gcd(int a, int b) { Â Â Â Â if (b == 0) Â Â Â Â Â Â Â Â return a; Â Â Â Â return __gcd(b, a % b); }Â
// Function to return the LCM of two numbersstatic int lcm(int x, int y){Â Â Â Â int temp = (x * y) / __gcd(x, y);Â Â Â Â return temp;}Â
// Function to print the required numbersstatic void findNumbers(int []a, int n, Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â int []b, int m){Â
    // To store the lcm of array a[] elements    // and the gcd of array b[] elements    int lcmA = 1, gcdB = 0;Â
    // Finding LCM of first array    for (int i = 0; i < n; i++)        lcmA = lcm(lcmA, a[i]);Â
    // Finding GCD of second array    for (int i = 0; i < m; i++)        gcdB = __gcd(gcdB, b[i]);Â
    // No such element exists    if (gcdB % lcmA != 0)     {        Console.Write("-1");        return;    }Â
    // All the multiples of lcmA which are    // less than or equal to gcdB and evenly    // divide gcdB will satisfy the conditions    int num = lcmA;    while (num <= gcdB)     {        if (gcdB % num == 0)            Console.Write(num + " ");        num += lcmA;    }}Â
// Driver codepublic static void Main(String[] args){Â Â Â Â int []a = { 1, 2, 2, 4 };Â Â Â Â int []b = { 16, 32, 64 };Â
    int n = a.Length;    int m = b.Length;Â
    findNumbers(a, n, b, m);}}Â
// This code is contributed by 29AjayKumar |
Javascript
<script>Â
// Javascript implementation of the approachÂ
// Function to find nth centered// tridecagonal numberfunction __gcd(a, b) {Â Â Â Â if (b == 0) Â Â Â Â Â Â Â Â return a; Â Â Â Â Â Â Â Â Â Â Â Â Â return __gcd(b, a % b); Â Â Â Â Â }Â
// Function to return the LCM of two numbersfunction lcm(x, y){Â Â Â Â var temp = (x * y) / __gcd(x, y);Â Â Â Â return temp;}Â
// Function to print the required numbersfunction findNumbers(a, n, b, m){         // To store the lcm of array a[] elements    // and the gcd of array b[] elements    var lcmA = 1, gcdB = 0;Â
    // Finding LCM of first array    for(var i = 0; i < n; i++)        lcmA = lcm(lcmA, a[i]);Â
    // Finding GCD of second array    for(var i = 0; i < m; i++)        gcdB = __gcd(gcdB, b[i]);Â
    // No such element exists    if (gcdB % lcmA != 0)     {        document.write("-1");        return;    }Â
    // All the multiples of lcmA which are    // less than or equal to gcdB and evenly    // divide gcdB will satisfy the conditions    var num = lcmA;    while (num <= gcdB)     {        if (gcdB % num == 0)            document.write(num + " ");                     num += lcmA;    }}Â
// Driver codevar a = [ 1, 2, 2, 4 ];var b = [ 16, 32, 64 ];Â
var n = a.length;var m = b.length;Â
findNumbers(a, n, b, m);Â
// This code is contributed by Ankita sainiÂ
</script> |
4 8 16
Â
Time Complexity: O(max(n,m) * log(min(a, b)))
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



