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 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<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 ?> |
Javascript
<script> // Javascript 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) { let ans = arr[0]; for (let 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 let lcm = lcmOfArray(arr,n); // One by one check if value of lcm / arr[i] // can be formed using prime numbers. for (let i = 0; i < n; i++) { // divide each element of array by LCM let val = lcm / arr[i]; // Use each input prime number to divide // the result to remove all factors of // input prime numbers for (let 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 let arr = [50, 200]; let prime = [2, 3]; let n = arr.length; let m = prime.length; if (checkArray(arr, prime, n, m)) document.write( "Yes" ); else document.write( "No" ); </script> |
Output:
Yes
Time Complexity: O(m x n)
Auxiliary Space: O(1)
This article is contributed by Aarti_Rathi and Niteesh kumar. If you like neveropen and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the neveropen 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.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!