# Check if elements of array can be made equal by multiplying given prime numbers

Given an array of integers and an array of prime numbers. The task is to find if it is possible to make all the elements of integer array equal by multiplying one or more elements from prime given array of prime numbers.

Examples:

```Input : arr[]   = {50, 200}
prime[] = {2, 3}
Output : Yes
We can multiply 50 with 2 two times
to make both elements of arr[] equal

Input : arr[]   = {3, 4, 5, 6, 2}
prime[] = {2, 3}
Output : No
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

We find LCM of all array elements. All elements can be made equal only if it is possible to convert all numbers to LCM. So we find the multiplier for each element so that we can make that element equal to LCM by multiplying that number. After that we find if numbers from given primes can form given multiplier.

Algorithm-
Step 1: Find LCM of all numbers in the array O(n)

Step 2 : For each number arr[i]
——– Divide LCM by arr[i]
——– Use each input prime number to divide the result to remove all factors of input prime numbers (can use modulo to check divisibility)
——– If left over number is not 1, return false;

Step 3 :
Return true

Below is the implementation of above algorithm.

## C++

 `// C++ program to find if array elements can ` `// be made same ` `#include ` `using` `namespace` `std; ` ` `  `// To calculate LCM of whole array ` `int` `lcmOfArray(``int` `arr[], ``int` `n) ` `{ ` `    ``int` `ans = arr[0]; ` `    ``for` `(``int` `i=1; i

## Java

 `// Java program to find if array  ` `// elements can be made same ` ` `  `class` `GFG ` `{ ` `    ``static` `int` `___gcd(``int` `a, ``int` `b) ` `    ``{ ` `        ``// Everything divides 0  ` `        ``if` `(a == ``0` `|| b == ``0``) ` `        ``return` `0``; ` `     `  `        ``// base case ` `        ``if` `(a == b) ` `            ``return` `a; ` `     `  `        ``// a is greater ` `        ``if` `(a > b) ` `            ``return` `___gcd(a - b, b); ` `        ``return` `___gcd(a, b - a); ` `    ``}  ` `     `  `    ``// To calculate LCM of whole array ` `    ``static` `int` `lcmOfArray(``int` `arr[], ``int` `n) ` `    ``{ ` `        ``int` `ans = arr[``0``]; ` `        ``for` `(``int` `i = ``1``; i < n; i++) ` `            ``ans = (arr[i] * ans)/ ___gcd(arr[i], ans); ` `        ``return` `ans; ` `    ``} ` `     `  `    ``// function to check possibility if we can make ` `    ``// all element same or not ` `    ``static` `boolean` `checkArray(``int` `arr[], ``int` `prime[],  ` `                                          ``int` `n, ``int` `m) ` `    ``{ ` `        ``// Find LCM of whole array ` `        ``int` `lcm = lcmOfArray(arr,n); ` `     `  `        ``// One by one check if value of lcm / arr[i] ` `        ``// can be formed using prime numbers. ` `        ``for` `(``int` `i = ``0``; i < n; i++) ` `        ``{ ` `            ``// divide each element of array by LCM ` `            ``int` `val = lcm / arr[i]; ` `     `  `            ``// Use each input prime number to divide ` `            ``// the result to remove all factors of ` `            ``// input prime numbers ` `            ``for` `(``int` `j = ``0``; j < m && val != ``1``; j++) ` `                ``while` `(val % prime[j] == ``0``) ` `                    ``val = val / prime[j]; ` `     `  `            ``// If the remaining value is not 1, then ` `            ``// it is not possible to make all elements ` `            ``// same. ` `            ``if` `(val != ``1``) ` `            ``return` `false``; ` `        ``} ` `     `  `        ``return` `true``; ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `main (String[] args) ` `    ``{ ` `        ``int` `arr[] = {``50``, ``200``}; ` `        ``int` `prime[] = {``2``, ``3``}; ` `        ``int` `n = arr.length; ` `        ``int` `m = prime.length; ` `     `  `        ``if``(checkArray(arr, prime, n, m)) ` `        ``System.out.print(``"Yes"``); ` `        ``else` `        ``System.out.print(``"No"``); ` `    ``} ` `} ` ` `  `// This code is contributed by Anant Agarwal. `

## Python3

 `# Python  program to find ` `# if array elements can ` `# be made same ` ` `  `def` `___gcd(a,b): ` `     `  `    ``# Everything divides 0  ` `    ``if` `(a ``=``=` `0` `or` `b ``=``=` `0``): ` `        ``return` `0` `  `  `    ``# base case ` `    ``if` `(a ``=``=` `b): ` `        ``return` `a ` `  `  `    ``# a is greater ` `    ``if` `(a > b): ` `        ``return` `___gcd(a``-``b, b) ` `    ``return` `___gcd(a, b``-``a) ` ` `  `# To calculate LCM of whole array ` `def` `lcmOfArray(arr,n): ` `     `  `    ``ans ``=` `arr[``0``] ` `    ``for` `i ``in` `range``(``1``,n): ` `        ``ans ``=` `(arr[i]``*``ans)``/``___gcd(arr[i], ans) ` `    ``return` `ans ` ` `  `  `  `# function to check possibility ` `# if we can make ` `# all element same or not ` `def` `checkArray(arr, prime, n, m): ` ` `  `    ``# Find LCM of whole array ` `    ``lcm ``=` `lcmOfArray(arr, n) ` `  `  `    ``# One by one check if ` `    ``# value of lcm / arr[i] ` `    ``# can be formed using prime numbers. ` `    ``for` `i ``in` `range``(n): ` ` `  `        ``# divide each element ` `        ``# of array by LCM ` `        ``val ``=` `lcm``/``arr[i] ` `  `  `        ``# Use each input prime ` `        ``# number to divide ` `        ``# the result to remove ` `        ``# all factors of ` `        ``# input prime numbers ` `        ``for` `j ``in` `range``(m ``and` `val!``=``1``): ` `            ``while` `(val ``%` `prime[j] ``=``=` `0``): ` `                ``val ``=` `val``/``prime[j] ` `  `  `        ``# If the remaining value ` `        ``# is not 1, then ` `        ``# it is not possible to ` `        ``# make all elements ` `        ``# same. ` `        ``if` `(val !``=` `1``): ` `            ``return` `0` `  `  `    ``return` `1` ` `  `# Driver code ` `arr ``=` `[``50``, ``200``] ` `prime ``=` `[``2``, ``3``] ` `n ``=` `len``(arr) ` `m ``=` `len``(prime) ` `  `  `if``(checkArray(arr, prime, n, m)): ` `    ``print``(``"Yes"``) ` `else``: ` `    ``print``(``"No"``) ` ` `  `# This code is contributed ` `# by Anant Agarwal. `

## C#

 `// C# program to find if array  ` `// elements can be made same ` `using` `System; ` ` `  `class` `GFG { ` `     `  `    ``static` `int` `___gcd(``int` `a, ``int` `b) ` `    ``{ ` `         `  `        ``// Everything divides 0  ` `        ``if` `(a == 0 || b == 0) ` `            ``return` `0; ` `     `  `        ``// base case ` `        ``if` `(a == b) ` `            ``return` `a; ` `     `  `        ``// a is greater ` `        ``if` `(a > b) ` `            ``return` `___gcd(a - b, b); ` `             `  `        ``return` `___gcd(a, b - a); ` `    ``}  ` `     `  `    ``// To calculate LCM of whole array ` `    ``static` `int` `lcmOfArray(``int` `[]arr, ``int` `n) ` `    ``{ ` `        ``int` `ans = arr[0]; ` `         `  `        ``for` `(``int` `i = 1; i < n; i++) ` `            ``ans = ((arr[i] * ans) /  ` `                       ``___gcd(arr[i], ans)); ` `        ``return` `ans; ` `    ``} ` `     `  `    ``// function to check possibility if  ` `    ``// we can make all element same or not ` `    ``static` `bool` `checkArray(``int` `[]arr,  ` `                 ``int` `[]prime, ``int` `n, ``int` `m) ` `    ``{ ` `         `  `        ``// Find LCM of whole array ` `        ``int` `lcm = lcmOfArray(arr, n); ` `     `  `        ``// One by one check if value of  ` `        ``// lcm / arr[i] can be formed  ` `        ``// using prime numbers. ` `        ``for` `(``int` `i = 0; i < n; i++) ` `        ``{ ` `             `  `            ``// divide each element of  ` `            ``// array by LCM ` `            ``int` `val = lcm / arr[i]; ` `     `  `            ``// Use each input prime number ` `            ``// to divide the result to ` `            ``// remove all factors of ` `            ``// input prime numbers ` `            ``for` `(``int` `j = 0; j < m && ` `                                ``val != 1; j++) ` `                ``while` `(val % prime[j] == 0) ` `                    ``val = val / prime[j]; ` `     `  `            ``// If the remaining value is not 1, ` `            ``// then it is not possible to make ` `            ``// all elements same. ` `            ``if` `(val != 1) ` `                ``return` `false``; ` `        ``} ` `     `  `        ``return` `true``; ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `Main () ` `    ``{ ` `        ``int` `[]arr = {50, 200}; ` `        ``int` `[]prime = {2, 3}; ` `        ``int` `n = arr.Length; ` `        ``int` `m = prime.Length; ` `     `  `        ``if``(checkArray(arr, prime, n, m)) ` `            ``Console.Write(``"Yes"``); ` `        ``else` `            ``Console.Write(``"No"``); ` `    ``} ` `} ` ` `  `// This code is contributed by nitin mittal `

## PHP

 ` ``\$b``) ` `        ``return` `___gcd(``\$a` `- ``\$b``, ``\$b``); ` `         `  `    ``return` `___gcd(``\$a``, ``\$b` `- ``\$a``); ` `}  ` ` `  `// To calculate LCM  ` `// of whole array ` `function` `lcmOfArray(``\$arr``, ``\$n``) ` `{ ` `    ``\$ans` `= ``\$arr``[0]; ` `     `  `    ``for` `(``\$i` `= 1; ``\$i` `< ``\$n``; ``\$i``++) ` `        ``\$ans` `= ((``\$arr``[``\$i``] * ``\$ans``) /  ` `                ``___gcd(``\$arr``[``\$i``], ``\$ans``)); ` `    ``return` `\$ans``; ` `} ` ` `  `// function to check  ` `// possibility if we ` `// can make all element ` `// same or not ` `function` `checkArray(``\$arr``, ``\$prime``,  ` `                    ``\$n``, ``\$m``) ` `{ ` `     `  `    ``// Find LCM of ` `    ``// whole array ` `    ``\$lcm` `= lcmOfArray(``\$arr``, ``\$n``); ` ` `  `    ``// One by one check if  ` `    ``// value of lcm / arr[i]  ` `    ``// can be formed using ` `    ``// prime numbers. ` `    ``for` `(``\$i` `= 0; ``\$i` `< ``\$n``; ``\$i``++) ` `    ``{ ` `         `  `        ``// divide each element ` `        ``// of array by LCM ` `        ``\$val` `= ``\$lcm` `/ ``\$arr``[``\$i``]; ` ` `  `        ``// Use each input prime  ` `        ``// number to divide the  ` `        ``// result to remove all  ` `        ``// factors of input prime ` `        ``// numbers ` `        ``for` `(``\$j` `= 0; ``\$j` `< ``\$m` `&& ` `                     ``\$val` `!= 1; ``\$j``++) ` `            ``while` `(``\$val` `% ``\$prime``[``\$j``] == 0) ` `                ``\$val` `= ``\$val` `/ ``\$prime``[``\$j``]; ` ` `  `        ``// If the remaining value  ` `        ``// is not 1, then it is  ` `        ``// not possible to make ` `        ``// all elements same. ` `        ``if` `(``\$val` `!= 1) ` `            ``return` `false; ` `    ``} ` ` `  `    ``return` `true; ` `} ` ` `  `// Driver code ` `\$arr` `= ``array``(50, 200); ` `\$prime` `= ``array``(2, 3); ` `\$n` `= sizeof(``\$arr``); ` `\$m` `= sizeof(``\$prime``); ` ` `  `if``(checkArray(``\$arr``, ``\$prime``,  ` `              ``\$n``, ``\$m``)) ` `    ``echo` `"Yes"``; ` `else` `    ``echo` `"No"``; ` ` `  `// This code is contributed ` `// by akt_mit ` `?> `

Output:

```Yes
```

This article is contributed by Niteesh kumar. 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.

My Personal Notes arrow_drop_up

Improved By : nitin mittal, jit_t, nidhi_biet

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.