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 numbers int lcm(int x, int y) { int temp = (x * y) / __gcd(x, y); return temp; } // Function to print the requried 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 requried 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 requried 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 requried 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 |
4 8 16
Recommended Posts:
- Queries for counts of multiples in an array
- Minimum sum after subtracting multiples of k from the elements of the array
- Sort elements of the array that occurs in between multiples of K
- Find the sum of prime numbers in the Kth array
- Prime factors of LCM of array elements
- Sum of element whose prime factors are present in array
- Find number of factors of N when location of its two factors whose product is N is given
- Find minimum value to assign all array elements so that array product becomes greater
- Find element in array that divides all array elements
- Sum of the multiples of two numbers below N
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Kth number from the set of multiples of numbers A, B and C
- Find the non decreasing order array from given array
- Find prime numbers in the first half and second half of an array
- N-th multiple in sorted list of multiples of two numbers
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.



