VOOZH about

URL: https://www.geeksforgeeks.org/dsa/check-elements-array-can-made-equal-multiplying-given-prime-numbers/

⇱ Check if elements of array can be made equal by multiplying given prime numbers - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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

Last Updated : 23 Jul, 2025

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.
 

Output: 

Yes

Time Complexity: O(m x n) 
Auxiliary Space: O(1)


 

Comment
Article Tags: