Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Check if elements of array can be made equal by multiplying given prime numbers in Python
Given two arrays nums and primes, we need to check whether all elements in nums can be made equal by multiplying each element with one or more prime numbers from the primes array.
For example, if nums = [25, 100] and primes = [2, 5], we can multiply 25 by 2 twice (25 × 2 × 2 = 100) to make both elements equal to 100.
Approach
The solution involves finding the LCM (Least Common Multiple) of all numbers in nums and checking if each element can be transformed to this LCM using only the given prime numbers ?
- Calculate the LCM of all elements in
nums - For each element, find the multiplication factor needed (LCM ÷ element)
- Check if this factor can be formed using only the given primes
- If all elements can be transformed, return
True
Implementation
from math import gcd
def array_lcm(nums):
"""Calculate LCM of all numbers in the array"""
ans = nums[0]
for i in range(1, len(nums)):
ans = (nums[i] * ans) // gcd(nums[i], ans)
return ans
def solve(nums, primes):
"""Check if elements can be made equal using given primes"""
lcm_arr = array_lcm(nums)
for i in range(len(nums)):
val = lcm_arr // nums[i] # Factor needed to reach LCM
# Try to reduce val using available primes
for j in range(len(primes)):
while val % primes[j] == 0:
val = val // primes[j]
# If val is not 1, some prime factors are missing
if val != 1:
return False
return True
# Test the function
nums = [25, 100]
primes = [2, 5]
result = solve(nums, primes)
print(f"Can elements be made equal? {result}")
Can elements be made equal? True
How It Works
Let's trace through the example step by step ?
nums = [25, 100]
primes = [2, 5]
# Step 1: Calculate LCM of [25, 100]
# LCM(25, 100) = 100
lcm_value = array_lcm(nums)
print(f"LCM of {nums} = {lcm_value}")
# Step 2: Check each element
for i, num in enumerate(nums):
factor = lcm_value // num
print(f"Element {num}: needs factor {factor} to reach LCM")
# Check if factor can be made from primes
temp_factor = factor
for prime in primes:
while temp_factor % prime == 0:
temp_factor = temp_factor // prime
print(f"Remaining factor after using primes: {temp_factor}")
LCM of [25, 100] = 100 Element 25: needs factor 4 to reach LCM Remaining factor after using primes: 1 Element 100: needs factor 1 to reach LCM Remaining factor after using primes: 1
Another Example
Let's test with a case that returns False ?
# Example where elements cannot be made equal
nums = [6, 15]
primes = [2, 3]
result = solve(nums, primes)
print(f"Can elements be made equal? {result}")
# Let's see why it fails
lcm_value = array_lcm(nums)
print(f"LCM = {lcm_value}")
for num in nums:
factor = lcm_value // num
print(f"{num} needs factor {factor}")
temp_factor = factor
for prime in primes:
while temp_factor % prime == 0:
temp_factor = temp_factor // prime
print(f"Remaining factor: {temp_factor}")
Can elements be made equal? False LCM = 30 6 needs factor 5 Remaining factor: 5 15 needs factor 2 Remaining factor: 1
Key Points
- The algorithm finds the target value (LCM) that all elements should reach
- For each element, it calculates the multiplication factor needed
- It verifies that each factor can be constructed using only the given prime numbers
- Time complexity: O(n × m × log(max_num)) where n is length of nums, m is length of primes
Conclusion
This solution efficiently checks if array elements can be made equal by finding the LCM and verifying that required multiplication factors can be formed using the given prime numbers. The key insight is that if any required factor contains prime numbers not in the given list, the transformation is impossible.
---