- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if elements of array can be made equal by multiplying given prime numbers in Python
Suppose we have two arrays, one is nums and another one is primes. We have to check whether it is possible to make all the elements of nums equal by multiplying one or more prime numbers from primes array.
So, if the input is like nums = [25, 100] primes = [2, 5], then the output will be True as we can multiply 25 with 2 twice to get 100 then all elements are same.
To solve this, we will follow these steps −
- lcm_arr := LCM of all elements in nums
- for i in range 0 to size of nums - 1, do
- val := lcm_arr/nums[i]
- if size of primes is not 0 and val is not 1, then
- while val mod primes[0] is 0, do
- val := val/primes[j]
- while val mod primes[0] is 0, do
- if val is not same as 1, then
- return False
- return True
Example
Let us see the following implementation to get better understanding −
from math import gcd def array_lcm(nums): ans = nums[0] for i in range(1,len(nums)): ans = (nums[i]*ans)/gcd(nums[i], ans) return ans def solve(nums, primes): lcm_arr = array_lcm(nums) for i in range(len(nums)): val = lcm_arr/nums[i] for j in range(len(primes) and val != 1): while (val % primes[j] == 0): val = val/primes[j] if (val != 1): return False return True nums = [25, 100] primes = [2, 5] print(solve(nums, primes))
Input
[25, 100], [2, 5]
Output
True
- Related Articles
- Check if elements of an array can be arranged satisfying the given condition in Python
- Check if a prime number can be expressed as sum of two Prime Numbers in Python
- Check if array sum can be made K by three operations on it in Python
- Check if given string can be formed by concatenating string elements of list in Python
- Check if a two-character string can be made using given words in Python
- Check if LCM of array elements is divisible by a prime number or not in Python
- Count of numbers which can be made power of 2 by given operation in C++
- Check if product of array containing prime numbers is a perfect square in Python
- Check if some elements of array are equal JavaScript
- Check if the array can be sorted using swaps between given indices only in Python
- Maximum elements that can be made equal with k updates in C++
- Check if an array contains all elements of a given range in Python
- Check if the elements of the array can be rearranged to form a sequence of numbers or not in JavaScript
- Check if the given array can be reduced to zeros with the given operation performed given number of times in Python
- Check if an array of 1s and 2s can be divided into 2 parts with equal sum in Python

Advertisements