Encuentre números que sean múltiplos de la primera array y factores de la segunda array

Dadas dos arrays A[] y B[] , la tarea es encontrar los números enteros que son divisibles por todos los elementos de la array A[] y dividir todos los elementos de la array B[] .

Ejemplos:  

Entrada: A[] = {1, 2, 2, 4}, B[] = {16, 32, 64} 
Salida: 4 8 16 
4, 8 y 16 son los únicos números que 
son múltiplos de todos los elementos de la array A[] 
y divide todos los elementos de la array B[]

Entrada: A[] = {2, 3, 6}, B[] = {42, 84} 
Salida: 6 42 
 

Enfoque: si X es un múltiplo de todos los elementos de la primera array, entonces X debe ser un múltiplo del MCM de todos los elementos de la primera array. 
De manera similar, si X es un factor de todos los elementos del segundo arreglo, entonces debe ser un factor del MCD de todos los elementos del segundo arreglo y tal X existirá solo si el MCD del segundo arreglo es divisible por el MCM de la primera array. 
Si es divisible, entonces X puede ser cualquier valor del rango [LCM, GCD] que es un múltiplo de LCM y divide uniformemente a GCD.

A continuación se muestra la implementación del enfoque anterior:  

C++

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the LCM of two numbers
int lcm(int x, int y)
{
    int temp = (x * y) / __gcd(x, y);
    return temp;
}
 
// Function to print the required numbers
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) {
        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 code
int 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 approach
import 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 numbers
static int lcm(int x, int y)
{
    int temp = (x * y) / __gcd(x, y);
    return temp;
}
 
// Function to print the required numbers
static 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 code
public 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 approach
using 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 numbers
static int lcm(int x, int y)
{
    int temp = (x * y) / __gcd(x, y);
    return temp;
}
 
// Function to print the required numbers
static 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 code
public 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 number
function __gcd(a, b)
{
    if (b == 0)
        return a;
         
    return __gcd(b, a % b);
     
}
 
// Function to return the LCM of two numbers
function lcm(x, y)
{
    var temp = (x * y) / __gcd(x, y);
    return temp;
}
 
// Function to print the required numbers
function 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 code
var 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>
Producción: 

4 8 16

 

Complejidad del tiempo: O(max(n,m) * log(min(a, b)))

Espacio Auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por souradeep y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *