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
We find LCM of all array elements. All elements can be made equal only if it is possible to covert 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<bits/stdc++.h> using namespace std; // To calculate LCM of whole array 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 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 int main() { int arr[] = {50, 200}; int prime[] = {2, 3}; int n = sizeof(arr)/sizeof(arr[0]); int m = sizeof(prime)/sizeof(prime[0]); checkArray(arr, prime, n, m)? cout << "Yes" : cout << "No"; return 0; } |
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
<?php // PHP program to find if // array elements can be // made same function ___gcd($a, $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 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.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Check if all the elements can be made equal on dividing with X and Y
- Check whether the sum of prime elements of the array is prime or not
- Check if LCM of array elements is divisible by a prime number or not
- Check if each element of the given array is the product of exactly K prime numbers
- Check if the array has an element which is equal to sum of all the remaining elements
- Check if product of array containing prime numbers is a perfect square
- Check if the array has an element which is equal to product of remaining elements
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Queries to check whether all the elements can be made positive by flipping signs exactly K times
- Check if a prime number can be expressed as sum of two Prime Numbers
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Sum of Semi-Prime Numbers less than or equal to N
- Print all Semi-Prime Numbers less than or equal to N
- Print prime numbers with prime sum of digits in an array
- Count numbers in a range having GCD of powers of prime factors equal to 1



